Strings

String definition

Before we progress any further, let’s look back on strings. As we’ve learnt before, string is a str data type value which represents a sequence made of characters.
Strings can be created by assignment, as we’ve seen. Strings spreading in a single line are written inside ” “ or ‘ ‘; if a string spreads in multiple lines, it’s written inside “”” “”” or ”’ ”’.
Strings can also be created using the command str().
>>> 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

Characters are components of strings, individually treated as strings of length 1. They represent symbols, each one with its own binary representation in the computer memory. In Python, characters are represented with 8 to 32 bits. There’s something called an ASCII table consisting of 128 basic characters and an Extended ASCII table consisting of 256 characters. Every character has an integer value.
In Python, we can use the command ord to see the integer value of each character. This function takes a single character as an argument ( if the argument doesn’t match a single character, the program signals an error). Opposite of ord, there is the command chr that returns a character with an integer value taken as the argument. If that value is greater-or-equal to 0x110000, the program will signal an error.
>>> 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

All relational operations can be applied on strings.
Operations == and != compare strings character by character.
Operations >, <, >= and <= work by comparing resulting values of commands ord(char1) and ord(char2), where char1 and char2 represent the first characters of two strings. If these values are equal, we compare next characters in line until the results are different or until there are no more characters in one of the strings.
>>> 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

This is a list of some useful (attribute) commands which can make the process of working with strings a bit easier:
str.lower() – returns a new string with all lowercase letters
str.upper() – returns a new string with all uppercase letters
str.swapcase() – returns a new string while turning lowercase letters to uppercase and vice versa
>>> str = 'New York City'
>>> print(str.lower(), str.upper(), str.swapcase())
new york city NEW YORK CITY nEW yORK cITY 
str.strip() – returns a new string without ‘ ‘ characters at the beginning and the end of the string
str.count(ss) – returns the number of appearances of the substring ss in string str
str.index(ss)/rindex(ss) – looks through the string str and returns the index of the first/last appearance of the first character in the substring ss
str.find(ss)/rfind(ss) – does the same thing as previous command, but also returns -1 if there are 0 appearances of the substring ss in the string str
>>> 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

Leave a Reply