Professional Documents
Culture Documents
1 Strings
1.1 Strings creation
Another type that is very useful to have in programming is a non-mathematical one: string, which
is often referred to by its abbreviation str. A string is a sequence of characters (keystrokes), and
these are enclosed between a pair of either single ' or triple quotation marks (quotes) '''; one
can also use " or """ to mark the start and end of the string. Each of the following is a string (the
print function is discussed below):
In [1]: st = "Hello"
print(st)
Hello
In [2]: st = 'Hello'
print(st)
Hello
In [3]: st = '''Hello'''
print(st)
Hello
1
Hello "Nathalie"
Hello "Nathalie"
TRY IT! Assign “Hello” to a variable with name s1 and “AIMS” to a variable s2. Verify that s1
and s2 have the type the type function.
In [9]: s1 = "Hello"
s2 = "AIMS"
In [10]: type(s1)
Out[10]: str
In [11]: type(s2)
Out[11]: str
In [12]: n = 1
type(n)
Out[12]: int
Convert n to a string
In [13]: s3 = str(n)
type(s3)
2
Out[13]: str
In [14]: s3
Out[14]: '1'
In [15]: n + s3
---------------------------------------------------------------------------
<ipython-input-15-74c05c789638> in <module>()
----> 1 n + s3
In [16]: TypeError?
In [17]: help(TypeError)
class TypeError(Exception)
| Inappropriate argument type.
|
| Method resolution order:
| TypeError
| Exception
| BaseException
| object
|
| Methods defined here:
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Methods inherited from BaseException:
|
| __delattr__(self, name, /)
3
| Implement delattr(self, name).
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __reduce__(...)
| helper for pickle
|
| __repr__(self, /)
| Return repr(self).
|
| __setattr__(self, name, value, /)
| Implement setattr(self, name, value).
|
| __setstate__(...)
|
| __str__(self, /)
| Return str(self).
|
| with_traceback(...)
| Exception.with_traceback(tb) --
| set self.__traceback__ to tb and return self.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from BaseException:
|
| __cause__
| exception cause
|
| __context__
| exception context
|
| __dict__
|
| __suppress_context__
|
| __traceback__
|
| args
Use the built-in function len to check the size of s1 and s2.
In [18]: len(s1)
Out[18]: 5
In [19]: len(s2)
4
Out[19]: 4
In [20]: s4 = ""
len(s4)
Out[20]: 0
Hello AIMS
Hello
AIMS
HelloAIMS
Hello
AIMS
5
In [27]: 2*s1
Out[27]: 'HelloHello'
In [28]: s1*s2
---------------------------------------------------------------------------
<ipython-input-28-7426096cfe41> in <module>()
----> 1 s1*s2
In the operation n*s where s is a string, n must be an integer and not a float!
In [29]: 2.*s1
---------------------------------------------------------------------------
<ipython-input-29-2c805062d9fc> in <module>()
----> 1 2.*s1
In [30]: s1
Out[30]: 'Hello'
In [31]: 'e' in s1
Out[31]: True
6
In [32]: 'el' in s1
Out[32]: True
In [33]: 'eo' in s1
Out[33]: False
In [34]: 'Lo' in s1
Out[34]: False
Out[35]: False
1.6 Slicing
1.6.1 Get one character
In [37]: s6[0]
Out[37]: 'I'
In [38]: s6[5]
Out[38]: 'e'
In [39]: s6[18]
Out[39]: '!'
In [40]: s6[-1]
Out[40]: '!'
In [42]: s6[:n]
7
1.6.3 Get the last n characters
In [43]: s6[-n:]
Out[43]: 'ming!'
In [46]: s6[:n]
In [47]: s6[::-1]
In [48]: s6[::2]
1.6.7 Replace
s.replace(s1,s2)
Note: The value of s is not changed!
In [49]: s6.replace("am","hello")
In [50]: s6
8
1.7 More operations
dir(str) or dir(s) where s is a string!
In [51]: dir(str)
Out[51]: ['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
9
'isalnum',
'isalpha',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']
In [52]: help(str.capitalize)
Help on method_descriptor:
capitalize(...)
S.capitalize() -> str
In [53]: "HELLO".capitalize()
Out[53]: 'Hello'
10
In [54]: help(str.startswith)
Help on method_descriptor:
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
In [55]: "hello".startswith("a")
Out[55]: False
In [56]: help(str.join)
Help on method_descriptor:
join(...)
S.join(iterable) -> str
11