String CheatSheet

Complete Python String Cheatsheet with clear examples for quick revision

1

Concatenation (+)

It joins a string with another string.

str1 = 'hi I am'
str2 = 'ayush'
result = str1 + str2  # 'hi I amayush'
2

Repetition (*)

It repeats a string.

str = 'ayush'
result = str * 3  # 'ayushayushayush'
3

Membership operator: in / not in

  • in: checks presence of a substring in a string.

  • not in: checks absence of a substring in a string.

str1 = 'hi I am Ayush'
result = 'I' in str1   # True

str1 = 'hi I am Ayush'
result = 'z' in str1   # False
4

len()

Gives the length (number of characters) of the string.

str1 = 'hi i am Ayush'
result = len(str1)  # 13
5

max()

Gives the maximum value character in a string.

str1 = 'Ayush'
result = max(str1)  # 'y'
6

min()

Gives the minimum value character in a string.

str1 = 'Ayush'
result = min(str1)  # 'A'
7

.lower()

Converts all characters of a string to lower case.

str1 = 'AYUSH'
result = str1.lower()  # 'ayush'
8

.upper()

Converts all characters of a string to upper case.

str1 = 'ayush'
result = str1.upper()  # 'AYUSH'
9

.capitalize()

Capitalizes the first character of the string only.

str1 = 'HI I AM AYUSH'
result = str1.capitalize()  # 'Hi i am ayush'
10

.title()

Capitalizes the first letter of each word in the string.

str1 = 'HI I AM AYUSH'
result = str1.title()  # 'Hi I Am Ayush'
11

.swapcase()

Converts lowercase characters to uppercase and uppercase to lowercase.

str1 = ' Hi I aM aYUsh'
result = str1.swapcase()  # 'hI i Am AyuSH'
12

.strip()

Removes whitespace from left and right sides of the string.

str1 = ' ayush '
result = str1.strip()  # 'ayush'
13

.lstrip()

Removes whitespace from the left side only.

str1 = ' ayush '
result = str1.lstrip()  # 'ayush '  (spaces on right remain)
14

.rstrip()

Removes whitespace from the right side only.

str1 = ' ayush '
result = str1.rstrip()  # ' ayush'  (spaces on left remain)
15

.replace(old, new)

Replaces all occurrences of the old substring with the new substring.

str1 = 'python is python and python is good.'
result = str1.replace('Python', 'java')  # Note: case-sensitive
# result: 'python is python and python is good.' (no change due to case)

Also:

str1 = 'python is python and python is good'
result = str1.replace('python', 'java', 2)
# 'java is java and python is good.'
16

.count('substring')

Gives the count of a substring in the string.

str1 = 'python is python and python is good'
result = str1.count('o')  # 5

Also with start/end:

str1 = 'python is python and python is good'
result = str1.count('o', 15, 35)  # 3
17

.find('substring')

Gives the index of the first appearance of the substring (or -1 if not found).

str1 = 'python is python and python is good'
result = str1.find('y')  # 1

With start/end:

str1 = 'python is python'
result = str1.find('y', 2, 15)  # 11
18

.rfind('substring')

Gives the index of the first appearance of the substring searching from the right.

str1 = 'python is python'
result = str1.rfind('y')  # 11
19

.index('substring')

Gives the index of the first appearance of the substring (raises ValueError if not found).

str1 = 'python is python and python is good'
result = str1.index('y')  # 1

With start/end:

str1 = 'python is python'
result = str1.index('y', 2, 15)  # 11
20

.rindex('substring')

Gives the index of the first appearance of the substring from the right (raises ValueError if not found).

str1 = 'python is python'
result = str1.rindex('y')  # 11
21

.split(separator)

Breaks a string into a list of substrings using the separator. Default separator is space.

str1 = 'python is a programming language'
result = str1.split()  # ['python', 'is', 'a', 'programming', 'language']

With maxsplit:

str1 = 'python is a programming language'
result = str1.split(' ', 2)  # ['python', 'is', 'a programming language']
22

.rsplit(separator)

Breaks a string into a list of substrings using the separator, starting from the right. Default separator is space.

str1 = 'python is a programming language'
result = str1.rsplit()  # ['python', 'is', 'a', 'programming', 'language']

With maxsplit:

str1 = 'python is a programming language'
result = str1.rsplit(' ', 2)  # ['python is a', 'programming', 'language']
23

.partition()

Splits the string at the first occurrence of the separator and includes the separator in the output.

str1 = 'python is a programming language'
result = str1.partition(' ')  # ('python', ' ', 'is a programming language')

Also:

str1 = 'python is a programming language'
result = str1.rpartition(' ')  # ('python is a programming', ' ', 'language')
24

.islower()

Checks whether all alphabetic characters in the string are lowercase.

str1 = 'ayush'
result = str1.islower()  # True
25

.isupper()

Checks whether all alphabetic characters in the string are uppercase.

str1 = 'AYUSH'
result = str1.isupper()  # True
26

.alpha()

Checks whether all characters in a string are alphabetic.

str1 = 'ayush'
result = str1.alpha()  # True

Note: the original example uses .alpha() as shown.

27

.isnumeric()

Checks whether all characters in a string are numeric digits.

str1 = '12345'
result = str1.isnumeric()  # True
28

.isalnum()

Checks whether all characters in a string are alphabetic or digits only.

str1 = 'ayush12345'
result = str1.isalnum()  # True
29

.startswith()

Checks whether the string starts with a particular substring.

str1 = '+91 1234567890'
result = str1.startswith('+91')  # True
30

.endswith()

Checks whether the string ends with a particular substring.

str1 = 'myfile.txt'
result = str1.endswith('txt')  # True
31

.isspace()

Checks whether the string contains only whitespace characters (space, tab, newline).

str1 = " "
result = str1.isspace()  # True
32

.istitle()

Checks whether the string follows title case (first letter of each word capital.

str1 = "Hi I Am Shikher"
str1 = "Hi I Am Shikher"
result = str1.istitle()  # True

str2 = "This is Python programming"
result = str2.istitle()  # False

33

.center(width, fillchar)

Centers the string by adding characters on both sides.

str2 = "Ayush"
result = str2.center(10, '*')  # **Ayush***

Explanation of parameters:

  • width → Total length of the final string

  • fillchar → Character used to fill empty spaces (must be one character)

34

.ljust(width, fillchar)

Aligns the string to the left and fills the right side.

str2 = "Ayush"
result = str2.ljust(10, '-')  # Ayush-----

Explanation of parameters:

  • width → Total length of the final string

  • fillchar → Character used to fill empty spaces (must be one character)

35

.rjust(width, fillchar)

Aligns the string to the right and fills the left side.

str2 = "Ayush"
result = str2.rjust(10, '-')  # -----Ayush

Explanation of parameters:

  • width → Total length of the final string

  • fillchar → Character used to fill empty spaces (must be one character)

String Formatting ( f" " )

Allows embedding variables directly inside strings using {}.

# Basic Example
name = "Ayush"
age = 61
print(f"My name is{name} and age is{age}")  # My name is Ayush and my age is 61
# Expression inside f-string
a = 10
b = 5
print(f"Sum is{a+b}")

String Slicing

str1 = "Programming"

Default Slicing:

str1[::]  # 'Programming'

Positive Index Slicing:

str1[0:6]  # 'Progra'

Negative Index Slicing:

Mixed Index Slicing:

Slicing with Step:

Reverse String (Negative Step):

Escape Characters

\n — New Line: Moves the cursor to the next line.

\t — Tab Space: Adds horizontal tab space (used for alignment).

\ — Backslash: Used to print a single backslash.

\' — Single Quote: Used to print a single quote inside single-quoted string.

\" — Double Quote: Used to print a double quote inside double-quoted string.

Last updated