Python: String

Strings in Python are ordered sequences of characters used to store and represent textual information. Therefore, strings can be used to work with anything that can be represented in text form. The length of a string in Python is limited only by the "amount of available memory". In Python 3, Unicode encoding is used for strings. The first 128 characters in the Unicode table are the same as in the ASCII character table.

When declaring strings, they need to be enclosed in quotes 'Hello'. In Python, strings can use single quotes (apostrophes), double quotes "Hello", and even triple quotes """Hello""" or '''Hello'''.

string index image

Code: Example String

s = '"Python"'
print(s)

# "Python"

s = "'Python'"
print(s) 

# 'Python'

s = """Text in
some
lines"""
print(s)

# Text in
# some
# lines
            

Special Characters

Strings can contain special characters, such as newline and so on. Let's look at some of them:

\n – newline

\r – carriage return

\t – tab character

\" – double quote

\' – apostrophe

\\ – backslash

All special characters in Python strings are interpreted automatically. However, if it is necessary to output them exactly as characters in the string, they are escaped using a backslash (\).

Code: Example Special Characters

print('Hello\nPython')
# Hello
# Python

print('Hello\\nPython')
#Hello\nPython

print('Hello'Python')
# SyntaxError: invalid syntax


print('Hello\'Python')
# Hello'Python
                

Suppressing Escaping r''

Besides the method mentioned above, there is another way to output a string without converting special characters. To do this, place the letter r before the string (before the quotes). This will allow the string to be displayed in its raw, unformatted form.

Code: string r''

s=r'D:\new\tutor'
print(s)

# D:\new\tutor 

Methods and Functions for Working with Strings

Python offers a plethora of diverse methods and functions for working with strings. Strings are immutable data types, so almost all methods return a new string as their value, rather than altering the current one. Let's present these methods in the form of a table.

Function, Method Description Example
s1 + s2 Concatenation (string addition). s1 = 'Hello'
s2 = 'Python'
print(s1 + ' ' + s2)
Output: Hello Python
s1 * n Repeat the string n times. s1 = 'Hello'
print(s1 * 3)
Output: HelloHelloHello
s[i] Access a character by index. In Python, character indexing starts from 0. You can only access a character, but you cannot change it by index. You can also use negative indexing, where -1 is the last character. s1 = 'Hello'
print(s1[1])
Output: e
print(s1[-1])
Output: o
Invalid: s[1] = 'e'
Results in an error
s[i:j:step] Extract a substring, slice.
i - starting index,
j - ending index (not included),
step - step.
s1 = 'Hello'
print(s1[1:3])
Output: el
print(s1[1:5:2])
Output: el
print(s1[:2])
Output: He
print(s1[2:])
Output: llo
print(s1[::-1])
Output: olleH
print(s1[-1:])
Output: o
print(s1[:-1])
Output: Hell
len(s) Length of the string (number of characters in the string). s1 = 'Hello'
print(len(s1))
Output: 5
in Checks if a substring is present in the string. Returns True if present, otherwise False. s1 = 'Hello'
print('el' in s1)
Output: True
s.find(str, [start], [end]) Search for a substring in the string. Returns the index of the first occurrence if found or -1 if not found.
start and end are optional parameters. If not specified, the search is from the beginning to the end of the string.
start is inclusive, end is exclusive.
s1 = 'Hello'
print(s1.find('el'))
Output: 1
s1 = 'Programming'
print(s1.find('r', 3, 9))
Output: 4
s1 = 'Programming'
print(s1.find('a', 0, 3))
Output: -1
s.rfind(str, [start], [end]) Same as find, but searches in the reverse direction. s1 = 'Hello'
print(s1.rfind('el'))
Output: 1
s.replace(pattern, replacement) Replace a pattern in the string. s1 = 'Hello'
print(s1.replace('o', 'a'))
Output: Hella
s.split(separator) Split the string by a separator. The result is a list of words. s1='Hello'
print(s1.split('e')) Output: ['H', 'llo']
s.isdigit() Checks if the string consists only of digits. Returns True if yes, otherwise False. s1 = 'a1'
print(s1.isdigit())
Output: False
s1 = '21'
print(s1.isdigit())
Output: True
s.isalpha() Checks if the string consists only of letters. Returns True if yes, otherwise False. s1 = 'a1'
print(s1.isalpha())
Output: False
s1 = 'ab'
print(s1.isalpha())
Output: True
s.isalnum() Checks if the string consists of digits or letters. Returns True if yes, otherwise False. s1 = 'a1'
print(s1.isalnum())
Output: True
s1 = 'ab_'
print(s1.isalnum())
Output: False
s.islower() Checks if the string is in lowercase. Returns True if yes, otherwise False. s1 = 'ab'
print(s1.islower())
Output: True
s1 = 'aB'
print(s1.islower())
Output: False
s.isupper() Checks if the string is in uppercase. Returns True if yes, otherwise False. s1 = 'AB'
print(s1.isupper())
Output: True
s1 = 'aB'
print(s1.isupper())
Output: False
s.istitle() Checks if the words in the string start with an uppercase letter. Returns True if yes, otherwise False. s1 = 'John Smith'
print(s1.istitle())
Output: True
s1 = 'john Smith'
print(s1.istitle())
Output: False
s.lower() Converts the string to lowercase. s1 = 'PYTHON'
print(s1.lower())
Output: python
s.startswith(str) Checks if the string s starts with the pattern str. s1 = 'python'
s2 = 'py'
print(s1.startswith(s2))
Output: True
s1 = 'Python'
s2 = 'py'
print(s1.startswith(s2))
Output: False
s.endswith(str) Checks if the string s ends with the pattern str. s1 = 'python'
s2 = 'on'
print(s1.endswith(s2))
Output: True
s.join(list) Joins a list into a string with a separator s. s = ':'
print(s.join(['1', '2', '3', '4']))
Output: 1:2:3:4
ord(char) Converts a character to its ASCII code. s1 = 'P'
print(ord(s1))
Output: 80
chr(number) Converts an ASCII code to a character. a = 80
print(chr(a))
Output: P
s.capitalize() Converts the first character of the string to uppercase and the rest to lowercase. s1 = 'иванов Иван Иванович'
print(s1.capitalize())
Output: Иванов иван иванович
s.count(str, [start], [end]) Returns the number of non-overlapping occurrences of the substring in the range [start, end] (default is 0 and the length of the string). s1 = 'Иванов иван иванович'
s2 = 'иван'
print(s1.count(s2))
Output: 2
Note the case sensitivity
s.lstrip([chars]) Removes characters chars from the beginning of the string. By default, removes spaces.
s.lstrip([chars]) Removes characters chars from the beginning of the string. By default, removes spaces. s = ' Python'
print(s.lstrip())
Output: 'Python'
s.rstrip([chars]) Removes characters chars from the end of the string. By default, removes spaces. s = 'Python '
print(s.rstrip())
Output: 'Python'
s.strip([chars]) Removes characters chars from both the beginning and the end of the string. By default, removes spaces. s = ' Python '
print(s.strip())
Output: 'Python'
s.swapcase() Converts lowercase characters to uppercase and vice versa. s = 'PyThOn'
print(s.swapcase())
Output: pYtHoN
s.title() Converting the first letter of each word to uppercase case, and all other letters are lowercase. s = 'john smith'
print(s.title())
Output: John Smith

Cyclic String Processing

Since a string is a sequence of characters, we can iterate through all characters using loops. Let's consider two ways to iterate through the characters of a string: Using a for loop and the function to find the length of the string.

Code: Cyclic String Processing

s = 'Python'
for i in range(len(s)):
    print(s[i], end=' ')

# P y t h o n
                

Using the for loop and its ability to work with any sequences (a string is a sequence).

Code: Cyclic String Processing

s = 'Python'
for i in s:
    print(i, end=' ')

# P y t h o n
        

Links: Python documentation


[1] Python string - methods, functions