Strings
String definition
>>> text1 = 'this is a string'
>>> text2 = "Assassin's Creed"
>>> text3 = """
This string
spreads through
four lines
of code
"""
>>> text4 = str('abcd')
Command len returns the length of its argument (number of characters) as an integer value. If the argument is not a string, or there is no argument, the program signals an error.
>>> len("abcdef")
6
>>> len("")
0
>>> len(14)
TypeError: object of type 'int' has no len()
>>> len()
TypeError: len() takes exactly one argument (0 given)
Characters
>>> len('a')
1
>>> ord('F')
70
>>> ord('ж') #this is a symbol from the cyrilic script
1078
>>> ord('abcd')
TypeError: ord() expected a character, but string of length 4 found
>>> chr(56)
'8'
>>> chr(1071)
'Я'
>>> chr(0x110000)
ValueError: chr() arg not in range(0x110000)
Strings are similar to lists in the aspect of accessing individual elements or slicing (accessing multiple elements), which is done with an index, a number representing the position of the element in the string whose value goes from 0 to n-1, n being the length of the string.
>>> text = "This world shall know pain"
>>> print(text[3:-5:3])
sodhln
>>> print(text[::-1]) #reversed order of the string
'niap wonk llahs dlrow sihT'
Strings are immutable objects, which means their value cannot be changed by slicing or accessing single characters ( if you try to do so, the program will signal an error).
>>> text = "Pain"
>>> text[0] = "G"
TypeError: 'str' object does not support item assignment
Operations with strings
These are the operations we can apply to strings: relational operations, membership operations and concatenation.
Relational operations
>>> str1 = 'Trivium'
>>> str2 = 'Breaking Benjamin'
>>> print(str1 == str2) #lengths are different
False
>>> print(str1 > str2) #ord('T') > ord('B')
True
Membership operations
We can check the membership of a character or another string in the specified string with the operations in and not in.
>>> string = "Boulevard of broken dreams"
>>> print("a" in string, "broken" in string, "z" not in string)
True True True
Concatenation
Concatenation is the operation of addition between two strings, where the second string is appended onto the first. Concatenation can be performed by addition and add-and-assign operations. String’s value can be changed through concatenation, as well as its id (unique identifier; see the example).
>>> str1 = 'Grand '
>>> str2 = 'Theft '
>>> str3 = 'Auto '
>>> str4 = '6'
>>> id(str1) """command id returns a random identity number, but that value is unique to the
variable in the program running at the moment"""
51302640
>>> str1 += str2
>>> print(str1)
Grand Theft
>>> id(str1) #id changes with the value change
50754096
>>> str1 = str1 + str3 + str4
>>> print(str1)
Grand Theft Auto 6
>>> id(str1) #id changed again
51300032
Iteration through string
We can use a for-loop to iterate through a string.
>>> word = "representation"
>>> counter = 0
>>> for single_character in word:
if single_character == 'e':
counter += 1
>>> print(counter)
3
Commands for working with strings
>>> str = 'New York City'
>>> print(str.lower(), str.upper(), str.swapcase())
new york city NEW YORK CITY nEW yORK cITY
>>> str = 'abcdabcdabcdabc'
>>> print(str.count('abc'))
4
>>> print(str.index('abc'), str.rindex('abc'))
0 12
>>> print(str.rfind('abcde'))
-1
–str.startswith(ss)/endswith(ss) – returns True if the string str begins/ends with the substring ss
>>> str = '12345678'
>>> print(str.startswitch('0'), str.endswitch('8'))
False True
–str.replace(ss1, ss2) – returns a new string where substring ss1 is replaced with substring ss2
>>> str = 'Donkey'
>>> print(str.replace('D', 'M'))
Monkey
–sep.join(str) – returns a new string which consists of the separator sep nested between every neighboring character pair from the string str
>>> str = '911'
>>> print("-".join(str))
9-1-1
Table of contents
- Functions
- Collections
- Exceptions
- Input & Output
- RegEx & PRNG
- Classes And Objects
- Popular Libraries
- Additional Problems