Strings Cheatsheet
Strings Cheatsheet
Strings
Strings
Python strings can be indexed using the same notation as lists, since strings are
lists of characters. A single character can be accessed with bracket notation
str = 'yellow'
( [index] ), or a substring can be accessed using slicing
str[1] # => 'e'
( [start:end] ).
str[-1] # => 'w'
Indexing with negative numbers counts from the end of the string.
str[4:6] # => 'ow'
str[:4] # => 'yell'
str[-3:] # => 'low'
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 1/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
String Concatenation
To combine the content of two strings into a single string, Python provides the
+ operator. This process of joining strings is called concatenation. x = 'One fish, '
y = 'two fish.'
z = x + y
print(z)
# Output: One fish, two fish.
In Python, the built-in len() function can be used to determine the length
of an object. It can be used to compute the length of strings, lists, sets, and other length = len("Hello")
countable objects.
print(length)
# Output: 5
IndexError
When indexing into a string in Python, if you try to access an index that doesn’t
exist, an IndexError is generated. For example, the following code fruit = "Berry"
would create an IndexError : indx = fruit[6]
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 2/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
Immutable strings
Strings are immutable in Python. This means that once a string has been
defined, it can’t be changed.
There are no mutating methods for strings. This is unlike data types like lists,
which can be modified once they are created.
Escaping Characters
Iterate String
str = "hello"
for c in str:
print(c)
# h
# e
# l
# l
# o
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 3/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
The in Syntax
print(greeting.lower())
# Prints: welcome to chili's
The string method .upper() returns the string with all lowercase
characters converted to uppercase. dinosaur = "T-Rex"
print(dinosaur.upper())
# Prints: T-REX
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 4/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
The string method .title() returns the string in title case. With title
case, the first character of each word is capitalized while the rest of the my_var = "dark knight"
characters are lowercase.
print(my_var.title())
print(text.split('i'))
# Prints: ['S', 'l', 'con Valley']
print(x)
# Prints: Codecademy-is-awesome
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 5/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
String replace
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 6/7
08.08.2020 Learn Python 3: Strings Cheatsheet | Codecademy
The Python string method .find() returns the index of the first
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 7/7