The basics of working with string in Python: Creation, methods and manipulations with text data

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A self-study guide for Python 3 compiled from the materials on this site. Primarily intended for those who want to learn the Python programming language from scratch.

Full table of string methods in Python

method
TheDescription Example
upper() Conversion to uppercase "hello".upper()"HELLO"
lower() Conversion to lowercase "HELLO".lower()"hello"
capitalize() The first letter is uppercase, the rest are lowercase "hello world".capitalize()"Hello world"
title() Every word with a capital letter "hello world".title()"Hello World"
swapcase() Case inversion "Hello".swapcase()"hELLO"
casefold() Aggressive lowercase conversion "ß".casefold()"ss"

Cleaning and leveling methods

method
TheDescription Example
strip() Removing spaces from the edges " hello ".strip()"hello"
lstrip() Removing spaces on the left " hello ".lstrip()"hello "
rstrip() Removing spaces on the right " hello ".rstrip()" hello"
strip(chars) Deleting specified characters "...hello...".strip(".")"hello"
center(width) Centering the row "hi".center(10)" hi "
ljust(width) Left alignment "hi".ljust(10)"hi "
rjust(width) Right alignment "hi".rjust(10)" hi"
zfill(width) Filling with zeros on the left "42".zfill(5)"00042"

Replacement and modification methods

method
TheDescription Example
replace(old, new) Substring replacement "hello".replace("l", "x")"hexxo"
replace(old, new, count) Quantity-limited replacement "hello".replace("l", "x", 1)"hexlo"
translate(table) Substitution according to the symbol table "hello".translate(str.maketrans("l", "x"))"hexxo"
expandtabs(tabsize) Replacing tabs with spaces "a\tb".expandtabs(4)"a b"

Separation and unification methods

method
TheDescription Example
split() Separation by spaces "a b c".split()["a", "b", "c"]
split(sep) Separation by separator "a,b,c".split(",")["a", "b", "c"]
split(sep, maxsplit) Split with a limit "a,b,c".split(",", 1)["a", "b,c"]
rsplit() Split on the right "a.b.c".rsplit(".", 1)["a.b", "c"]
splitlines() Line division "a\nb\nc".splitlines()["a", "b", "c"]
partition(sep) Split into 3 parts "hello-world".partition("-")("hello", "-", "world")
rpartition(sep) Dividing the right into 3 parts "a.b.c".rpartition(".")("a.b", ".", "c")
join(iterable) Combining the list ",".join(["a", "b", "c"])"a,b,c"

Search methods

method
TheDescription Example
find(sub) Substring search (index) "hello".find("ll")2
find(sub, start) Search from the starting position "hello".find("l", 3)3
find(sub, start, end) Range search "hello".find("l", 0, 2)-1
rfind(sub) Search on the right "hello".rfind("l")3
index(sub) Search with exception "hello".index("ll")2
rindex(sub) Search on the right with an exception "hello".rindex("l")3
count(sub) Counting occurrences "hello".count("l")2
count(sub, start, end) Counting in the range "hello".count("l", 0, 3)1

Methods for checking the beginning and end

method
TheDescription Example
startswith(prefix) Checking the start "hello".startswith("he")True
startswith(tuple) Checking multiple prefixes "hello".startswith(("he", "hi"))True
endswith(suffix) Checking the end "hello".endswith("lo")True
endswith(tuple) Checking multiple suffixes "hello".endswith(("lo", "la"))True

Character type verification methods

method
TheDescription Example
isdigit() Checking for numbers "123".isdigit()True
isalpha() Checking for letters "abc".isalpha()True
isalnum() Checking for letters and numbers "abc123".isalnum()True
isspace() Checking for whitespace characters " ".isspace()True
islower() Checking for lowercase "hello".islower()True
isupper() Upper case check "HELLO".isupper()True
istitle() Checking for header case "Hello World".istitle()True
isascii() Checking for ASCII characters "hello".isascii()True
isdecimal() Checking for decimal numbers "123".isdecimal()True
isnumeric() Checking for numeric characters "123".isnumeric()True
isidentifier() Checking for a valid ID "var_name".isidentifier()True
isprintable() Checking for printable characters "hello".isprintable()True

Coding methods

method
TheDescription Example
encode() Encoding in bytes "hello".encode()b'hello'
encode(encoding) Encoding in the specified encoding "hello".encode("utf-8")b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'

Formatting methods

method
TheDescription Example
format(*args, **kwargs) String formatting "Hello {}".format("world")"Hello world"
format_map(mapping) Formatting via dictionary "Hello {name}".format_map({"name": "world"})"Hello world"

All these methods return a new string without changing the original one, since strings in Python are immutable.

Creating strings in Python

Strings in Python are ordered sequences of characters and are one of the main data types. They can be created in several ways:

# Single quotes
my_string = 'Hello, world!'

# Double quotes
my_string = "Hello, world!"

# Triple quotes for multiline strings
my_string = "'This is a multi
-line string in Python"'

# Triple double quotes
my_string = """Another way
to create a multiline string"""

Indexing and access to characters

In Python, each character of a string has its own index, starting from 0. Both positive and negative indexing is supported:

my_string = "Python"
print(my_string[0]) #P (first character)
print(my_string[-1]) # n (last character)
print(my_string[2]) # t (third character)
print(my_string[-2]) # o (penultimate character)

Slicing lines

Slices allow you to extract substrings from a string. Syntax: string[start:stop:step]

my_string = "Hello, World!"

# Basic slices
print(my_string[7:])     # World! (from the 7th character to the end)
print(my_string[:5]) # Hello (from the beginning to the 5th character)
print(my_string[2:8]) # llo, W (from the 2nd to the 8th character)

# Slices in increments
print(my_string[::2])    # Hlo ol! (every second character)
print(my_string[2::3]) # l r! (from the 2nd character in increments of 3)

# Reverse order
print(my_string[::-1]) # !dlroW ,olleH (the whole string is in reverse order)

# Negative indexes in slices
print(my_string[-6:-1]) # World(from -6th to -1st character)
print(my_string[-6:]) # World! (from -6th character to the end)
print(my_string[-1:-7:-1]) # ! dlroW (reverse slice)

Basic string operations

Determining the length of a string

my_string = "Hello, World!"
print(len(my_string))  # 13

Concatenation (concatenation) of strings

string1 = "Hello"
string2 = "World"
result = string1 + ", " + string2 + "!"
print(result)  # Hello, World!

# Repeating the string
repeated = "Python " * 3
print(repeated) # Python Python Python

Checking the substring content

text = "Python is a powerful programming language"
print("Python" in text)        # True
print("Java" not in text)      # True
print("powerful" in text)        # True

String formatting

Format() method

name = "Anna"
age = 25
city = "Moscow"

# Positional arguments
formatted = "My name is {}, I am {} years old, I live in {}".format(name, age, city)
print(formatted)

# Named arguments
formatted = "My name is {name}, I am {age} years old."format(name=name, age=age)
print(formatted)

# Indexes
formatted = "My name is {0}, I am {1} years old, I live in {2}".format(name, age, city)
print(formatted)

F-strings (formatted string literals)

name = "Anna"
age = 25
print(f"My name is {name}, I am {age} years old")

# Expressions in f-lines
print(f"In 5 years I will be {age + 5} years old")

# Formatting numbers
price = 1234.5678
print(f"Price: {price:.2f} rubles") # Price: 1234.57 rubles

Old formatting style (%)

name = "Anna"
age = 25
print("My name is %s, I am %d years old" % (name, age))

Basic string methods

Case change methods

text = "Python Programming"

print(text.upper()) # PYTHON PROGRAMMING
print(text.lower()) # python programming
print(text.capitalize()) # Python programming
print(text.title()) # Python Programming
print(text.swapcase()) # pYTHON pROGRAMMING
print(text.casefold()) # python programming (more aggressive lowercase reduction)

Character type verification methods

# Checking for numbers
print("123".isdigit())    # True
print("12.3".isdigit())   # False

# Checking for letters
print("abc".isalpha())    # True
print("abc123".isalpha()) # False

# Checking for letters and numbers
print("abc123".isalnum()) # True
print("abc 123".isalnum()) # False

# Checking the register
print("hello".islower())  # True
print("HELLO".isupper())  # True
print("Hello World".istitle()) # True

# Checking for whitespace characters
print("   ".isspace())    # True
print("\t\n".isspace())   # True

Search and replace methods

text = "Python is the best programming language for learning Python"

# Substring search
print(text.find("Python"))      #0 (first occurrence)
print(text.rfind("Python"))     #58 (last occurrence)
print(text.find("Java"))        # -1 (not found)

# Safer search (throws exception if not found)
try:
print(text.index("Python"))  # 0
    print(text.rindex("Python")) # 58
except ValueError:
    print("Substring not found")

# Counting occurrences
of print(text.count("Python"))     # 2

# Substring replacement
new_text = text.replace("Python", "Java")
print(new_text)

# Limited replacement
new_text = text.replace("Python", "Java", 1) # Replace only the first occurrence
of print(new_text)

Splitting and combining methods

text = "apple,banana,orange,pear"

# Splitting the string
fruits = text.split(",")
print(fruits) # ['apple', 'banana', 'orange', 'pear']

# Partitioning with restriction
fruits = text.split(",", 2)
print(fruits) # ['apple', 'banana', 'orange,pear']

# Splitting by lines
multiline = "First line\Second line\ Third line"
lines = multiline.splitlines()
print(lines) # ['First line', 'Second line', 'Third line']

# Combining a list into a string
words = ["Python", "this", "excellent", "language"]
sentence = " ".join(words)
print(sentence) # Python is a great language

# Splitting into parts
text = "name:age:city"
parts = text.partition(":")
print(parts) # ('name', ':', 'age:city')

# Splitting from the end
parts = text.rpartition(":")
print(parts) # ('name:age', ':', 'city')

String cleaning methods

text = "   Python Programming   "

# Removing spaces
print(text.strip()) # "Python Programming"
print(text.lstrip()) # "Python Programming   "
print(text.rstrip())  # "   Python Programming"

# Removing certain characters
text = "...Python..."
print(text.strip("."))  # "Python"

# Deleting multiple characters
text = "!@#Python#@!"
print(text.strip("!@#"))  # "Python"

Alignment methods

text = "Python"

# Center alignment
print(text.center(20))      # "       Python       "
print(text.center(20, "*")) # "*******Python*******"

# Left alignment
print(text.ljust(20)) # "Python "
print(text.ljust(20, "-")) # " Python--------------"

# Right alignment
print(text.rjust(20)) # " Python"
print(text.rjust(20, "-")) # "-------------- Python"

# Zero padding
number = "42"
print(number.zfill(5)) # "00042"

Methods for checking the beginning and end of a line

filename = "document.pdf"

print(filename.startswith("doc"))    # True
print(filename.endswith(".pdf"))     # True
print(filename.endswith((".pdf", ".doc", ".txt"))) # True (you can pass a tuple)

# Range verification
text = "Python Programming"
print(text.startswith("Pro", 7)) # True (starting from the 7th character)
print(text.endswith("gram", 0, 14)) # True (up to the 14th character)

Working with Unicode and encodings

# Getting the character code
print(ord('A'))  #65
print(ord('I')) # 1071

# Getting a character by code
print(chr(65)) # A
print(chr(1071)) # I

# Working with Unicode
text = "Hello, world! ?"
print(len(text))  #13 (including emojis)

# Encoding and decoding
text = "Hello"
encoded =text.encode('utf-8')
print(encoded) # b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
decoded= encoded.decode('utf-8')
print(decoded) # Hello

Character escaping

# Special characters
print("String with \"quotes\"")
print('String with \'quotes\")
print("String with \\ backslash")
print("String with \n newline")
print("String with \t tabs")

# Raw strings (without escaping)
print(r"C:\Users\name\Documents ") # C:\Users\name\Documents
print(r"String with \n without newline")  # A string with \n without a newline

String comparison

# Lexicographic comparison
print("apple" < "banana")  # True
print("Apple" < "apple")   # True (capital letters have a smaller code)

# Case-insensitive comparison
str1 = "Python"
str2 = "python"
print(str1.lower() == str2.lower())  # True

# Numerical comparison
print("10" > "2") # False (string comparison)
print(int("10") > int("2")) # True(numerical comparison)

Useful practical examples

Data validation

def validate_email(email):
"""Simple email validation"""
return"@" in email and "." in email.split("@")[-1]

def validate_phone(phone):
    """Checking the phone number (digits only)"""
clean_phone = phone.replace("-", "").replace(" ", "").replace("(", "").replace(")", "")
return clean_phone.isdigit() and len(clean_phone) >= 10

# Usage examples
print(validate_email("user@example.com"))  # True
print(validate_phone("+7 (123) 456-78-90")) # False (contains +)

Text processing

def clean_text(text):
"""Cleaning and normalizing text"""
# Removing extra spaces
text = " ".join(text.split())
# Lowercase
    text = text.lower()
# Removing punctuation marks (simple example)
import string
    text = text.translate(str.maketrans("", "", string.punctuation))
    return text

text = " Hello, WORLD!!!  How are you?  "
print(clean_text(text))  # hello world how are you

Text generation

def generate_report(name, sales_data):
"""Generating a sales report"""
total_sales = sum(sales_data.values())
best_month = max(sales_data, key=sales_data.get)
    
    report = f"""
Sales report for {name}
    {'=' * 30}
    
    Total sales amount: {total_sales:,.2f} rub.
    Best month: {best_month} ({sales_data[best_month]:,.2f} rub.)
    
    Details by month:
"""
    
    for month, amount in sales_data.items():
report +=f" {month}: {amount:,.2f} rub\n"
    
    return report

sales = {"January": 125,000, "February": 140,000, "March": 98,000}
print(generate_report("Horns and Hooves LLC", sales))

Table of basic string methods

method
TheDescription Example
upper() Conversion to uppercase "hello".upper()"HELLO"
lower() Conversion to lowercase "HELLO".lower()"hello"
capitalize() The first letter is uppercase "hello".capitalize()"Hello"
title() Every word with a capital letter "hello world".title()"Hello World"
strip() Removing spaces from the edges " hello ".strip()"hello"
replace(old, new) Substring replacement "hello".replace("l", "x")"hexxo"
split(sep) Splitting a line "a,b,c".split(",")["a", "b", "c"]
join(iterable) Combining the list ",".join(["a", "b"])"a,b"
find(sub) Substring search "hello".find("ll")2
count(sub) Counting occurrences "hello".count("l")2
startswith(prefix) Checking the start "hello".startswith("he")True
endswith(suffix) Checking the end "hello".endswith("lo")True
isdigit() Checking for numbers "123".isdigit()True
isalpha() Checking for letters "abc".isalpha()True
isalnum() Checking for letters and numbers "abc123".isalnum()True
Strings in Python are immutable objects, so all methods return a new string without changing the original one. This is important to keep in mind when working with large amounts of text data to optimize application performance.

categories

  • Introduction to Python
  • Python Programming Basics
  • Control Structures
  • Data Structures
  • Functions and Modules
  • Exception Handling
  • Working with Files and Streams
  • File System
  • Object-Oriented Programming (OOP)
  • Regular Expressions
  • Additional Topics
  • General Python Base