Last Updated:

Functions and methods of processing strings in Python

Python

General Operations

  • String concatenation:
    word_first = 'Petya'
    word_second = 'Masha'
    word_third = word_first + ' ' + word_second # Petya Masha
  • Duplication, multiplication, repetition of lines:
    word_first = 'Petya'
    word_second = word_first * 2 # 'PetyaPetya
  • Convert characters to uppercase:
    word_first = 'Petya'
    print(word_second.upper()) # PETYA
  • Converting characters to lowercase:
    word_first = 'Petya'
    print(word_second.lower()) # petya
  • Convert each first letter of a word in a string to uppercase:
    word_first = 'My name is Petya'.title()
    print(word_first) # My Name Is Petya
  • To remove space characters at the beginning of a line:
    word_first = '    Petya    '.lstrip()
    print(word_first) # Petya   
  • Remove space characters at the end of a line:
    word_first = '    Petya    '.rstrip()
    print(word_first) #    Petya
  • Remove space characters at the beginning and end of a line:
    word_first = '    Petya    '.strip()
    print(word_first) # Petya
  • To convert lowercase characters to uppercase and uppercase characters to lowercase:
    word_first = 'Petya'.swapcase()
    print(word_first) # pETYA
  • Convert the first character of a string to uppercase and all remaining characters to lowercase:
    word_first = 'Petya - VERY GOOD!'.capitalize()
    print(word_first) # Petya - very good!

Compute Operations

  • Calculation of length, number of characters in a string:
    word_first = 'Petya'
    length_word = len(word_first) # 5

Cuts, cuts

  • Getting, extracting a character from a string:
    word_first = 'Petya'
    symbol_first = word_first[1] # e
    symbol_second = word_first[4] # a
    symbol_third = word_first[-1] # a
  • Slice a row, extract a sequence in a row:
    word_first = 'Petya'
    word_second = word_first[0:2] # Pe
    word_third = word_first[:4] # Pety
    word_fourth = word_first[1:] # etya
    word_fifth = word_first[-2:] # ya
  • Turning, reversing the line:
    word_first = 'Petya'
    word_second = sord_first[::-1] # ayteP
  • Extract every second character starting with the character number one:
    word_first = 'Petya'
    word_second = word_first[1::2] # ey

Formatting

  • Method one, pristine, productive:
    word_first = 'Hello, %s!' % 'Petya' # Hello, Petya!

     

    • '%d', '%i', '%u' is a decimal number;
    • '%o' is a number in the octal number system;
    • '%x' is a number in hexadecimal or lowercase letters;
    • '%X' is a number in hexadecimal or uppercase letters;
    • '%e' is a floating-point and exponent number (lowercase exponent);
    • '%E' is a floating-point number with an exponent (uppercase exponent);
    • '%f', '%F' — floating-point number in the usual form;
    • '%c' — a string of a single character or character code;
  • Method two, convenient, understandable:
    word_first = 'My name is {0}!'
    content = 'Petya'
    word_second = word_first.format(content) # My name is Petya
    word_third = '{0} {1} {3}!'.format('This', 'is', 'text') # This is text!
  • Method three, familiar, brief:
    word_first = 'Petya'
    age = 1963
    man = f'{word_first}, {age}' # Petya, 1963

Search

  • Search for a substring in a string (get the index of the beginning of the substring):
    word_first = 'Petya'
    index_first = word_first.find('ya') # 3
    index_second = word_first.find('Masha') # -1
  • Search for a substring in a string (get the index of the end of the substring):
    word_first = 'Petya'
    index_first = word_first.rfind('ya') # 4
    index_second = word_first.rfind('Masha') # -1
  • Whether the string consists of a numeric value:
    word_first = '123532'
    print(word_first.isdigit()) # True
    word_second = 'Petya'
    print(word_second.isdigit()) # False
  • Does the string consist of letters:
    word_first = '123532'
    print(word_first.isalpha()) # False
    word_second = 'Petya'
    print(word_second.isalpha()) # True
  • Whether the string consists of numbers or letters:
    word_first = '123532'
    print(word_first.isalnum()) # True
    word_second = 'Petya'
    print(word_second.isalnum()) # True
    word_third = '1234 Petya'
    print(word_second.isalnum()) # False
  • Whether the string consists of uppercase characters:
    print('Petya'.islower()) # False
    print('hello'.islower()) # False
    print('GOOD'.islower()) # True
  • Whether the string consists of lowercase characters:
    print('Petya'.islower()) # False
    print('hello'.islower()) # True
    print('GOOD'.islower()) # False
  • Do words in a line begin with a capital letter:
    print('Petya'.istitle()) # True
    print('hello'.istitle()) # False

Cut a string by character, replace characters

  • To replace a character or group of characters in a string:
    word_first = 'Petya'
    word_second = word_first.replace('e', 'i') # Pitya
    word_third = word_first.replace('et', '1234') # P1234ya
  • To cut a string by character:
    word_first = 'Petya + Masha'
    word_second = word_first.split(' ') # ['Petya', '+', 'Masha']
    word_third = word_first.split('a') # ['Pety', ' + M', 'sh', '']