The Python string data type is a sequence made up of one or more individual characters that could consist of letters, numbers, whitespace characters, or symbols. Because a string is a sequence, it can be accessed in the same ways that other sequence-based data types are, through indexing and slicing.
r'ABC' - raw string. Python raw string treats backslash (\
) as a literal character
u'ABC' or just 'ABC' - unicode string. Sequence of unicode characters. Each character may be encoded in several bytes
b'ABC' - bytes string. Each element is a byte or char. Can be stored on disk
f'ABC' - format string. Designed to apply formatting techniques
Raw String
print(r'a\nb') # escaping \
print('a\nb') # reading \n as new-line symbol
Encode / Decode
# Unicode to bytes
a = 'Строка на русском'
a.encode()
# Bytes to Unicode
b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'.decode ()
Format string
# Formatting string without format or %. No more code duplication!
a = 'some variable'
print(f'y = {a + " Y"} + x')
print(a + " Y")
Several arithmetic operations available when working with strings
# Concatenation
'abc' + 'def'
# Duplication
'abc' * 3
len('abcdef')
list('abcdef')
a = 'abcdef'
for symbol in a:
print(symbol)
for index in range(len(a)):
print(index, a[index])
Result of all operations on string is another string. It is impossible to change the string in-place
a = 'abcdef'
# 012345
# a[0] = 'A'
var = 1000
b = 'A' + a[1:]
We can also call out a range of characters from the string. With slices, we can call multiple character values by creating a range of index numbers separated by a colon [x:y]
# selections and slices
a = 'abcdef'
a[4]
# Up to the fourth symbol
a[:4]
# Indexing from the end
a[-3]
We can also specify the step of the slice. This parameter is called stride
# Step of the slice
a[1:4:2]
# String revert
a[::-1]
# joining string
",".join(['Hello', 'student'])
# splitting string
a = list("Hello, student")
a.split()
# upper
a.upper()
# lower
a.lower()
a = "mysevEralwordtitle"
a.title()
# Change case
a.swapcase()
# is alphabetic symbol
a.isalpha()
a[0].isdigit()
# Number of occurrences of substring
a.count('e')
# Return index of first occurrence
a.find('e')
a.replace('e', 'AAAAA')
b_string = a
'a' not in b_string
'Z' not in a
# Remove trailing symbols
a = ' some noizy string \n'
print(a.strip())
a = '!!!!!some noizy string!!!!!!!'
a.rstrip('\n')
index = a.find('s')
Repetition encoding is one of the ways to compress strings: sequence of the same elements is substituted with one symbol concatenated with the number of repetitions.
'A6B5C11' -> 'AAAAAABBBBBCCCCCCCCCCC'
Let's suppose the string may contain only alphabetic symbols A-Z or a-z
Read an input string, compressed using repetition encoding and decode it.
Numbers are 1-9
Any numbers
a.isalpha() # check if all characters of a string are letters
a.isdigit() # check if all characters of a string are digits
Find a second maximum in a sequence of numbers (like previously, numbers are entered, 0 means end of sequence, 0 is not a part of the sequence)
Input example code is available below
current = int(input())
minimum = current
while current != 0:
# your code
current = int(input())
print(minimum)