You are on page 1of 11

04-Strings

September 22, 2021

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

In [4]: st1 = "Hello Nathalie! We are exhausted"


print(st1)

Hello Nathalie! We are exhausted

What if the string contains a double quote "

In [5]: st2 = 'Hello "Nathalie"'


print(st2)

1
Hello "Nathalie"

In [6]: st3 = '''Hello "Nathalie"'''


print(st3)

Hello "Nathalie"

What if the string contains a single quote '

In [7]: st4 = 'Hello Class of '2020''

File "<ipython-input-7-ada9c8dbeb83>", line 1


st4 = 'Hello Class of '2020''
ˆ
SyntaxError: invalid syntax

In [8]: st5 = "Hello Class of '2020'"


print(st5)

Hello Class of '2020'

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

Create a variable n and assign to it the integer value 1

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'

If you try to add a string to a number, you get an error!

In [15]: n + s3

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-15-74c05c789638> in <module>()
----> 1 n + s3

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [16]: TypeError?

In [17]: help(TypeError)

Help on class TypeError in module builtins:

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

TRY IT! Create an empty string and check its size.

In [20]: s4 = ""
len(s4)

Out[20]: 0

In [21]: s4 = "a 1"


len(s4)
s4

Out[21]: 'a 1'

1.2 Escape sequence


The backslash \, also called the escape character, is used in representing certain whitespace
characters: \t"represents a tab and \n represents a new line.
Note: \ can also be used to escape itself!

In [22]: s5 = "Hello \" AIMS"


s5

Out[22]: 'Hello " AIMS'

In [23]: s5 = "Hello \t AIMS"


print(s5)

Hello AIMS

In [24]: s5 = "Hello \nAIMS"


print(s5)

Hello
AIMS

1.3 Operations on strings: + and *


In [25]: print(s1 + s2)

HelloAIMS

In [26]: print(s1 + "\n" + s2)

Hello
AIMS

5
In [27]: 2*s1

Out[27]: 'HelloHello'

Note: You cannot multiply two strings!

In [28]: s1*s2

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-28-7426096cfe41> in <module>()
----> 1 s1*s2

TypeError: can't multiply sequence by non-int of type 'str'

In the operation n*s where s is a string, n must be an integer and not a float!

In [29]: 2.*s1

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-29-2c805062d9fc> in <module>()
----> 1 2.*s1

TypeError: can't multiply sequence by non-int of type 'float'

1.4 Membership Testing


The in operator tests to check whether object x equals any item in the sequence S as follows: x in
S. It returns True if it at least one such item is found and False if not. The reverse test can be done
with the not in operator simply as x not in S.

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

In [35]: 'o' not in s1

Out[35]: False

1.5 Accessing elements by index


Let us assign to a string s the value "I love programming!"

In [36]: s6 = "I love programming!"

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]: '!'

1.6.2 Get the first n characters


In [41]: n = 5
s6[0:n]

Out[41]: 'I lov'

In [42]: s6[:n]

Out[42]: 'I lov'

7
1.6.3 Get the last n characters
In [43]: s6[-n:]

Out[43]: 'ming!'

1.6.4 Get all characters starting from the nth character


In [44]: s6[n:]

Out[44]: 'e programming!'

1.6.5 Get all characters before the nth character


In [45]: s6[:-n]

Out[45]: 'I love program'

In [46]: s6[:n]

Out[46]: 'I lov'

1.6.6 Reverse all characters


print(string[::-1])

In [47]: s6[::-1]

Out[47]: '!gnimmargorp evol I'

In [48]: s6[::2]

Out[48]: 'Ilv rgamn!'

1.6.7 Replace
s.replace(s1,s2)
Note: The value of s is not changed!

In [49]: s6.replace("am","hello")

Out[49]: 'I love progrhelloming!'

In [50]: s6

Out[50]: 'I love programming!'

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

Return a capitalized version of S, i.e. make the first character


have upper case and the rest lower case.

In [53]: "HELLO".capitalize()

Out[53]: 'Hello'

10
In [54]: help(str.startswith)

Help on method_descriptor:

startswith(...)
S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False otherwise.


With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.

In [55]: "hello".startswith("a")

Out[55]: False

In [56]: help(str.join)

Help on method_descriptor:

join(...)
S.join(iterable) -> str

Return a string which is the concatenation of the strings in the


iterable. The separator between elements is S.

11

You might also like