Learn Python 3 - Strings Reference Guide - Codecademy
Learn Python 3 - Strings Reference Guide - Codecademy
Learn Python 3
Strings
Print cheatsheet
Strings
In computer science, sequences of characters are referred to as strings. Strings
can be any length and can include any character such as letters, numbers,
symbols, and whitespace (spaces, tabs, new lines).
Escaping Characters
Backslashes ( \ ) are used to escape characters in a Python string.
For instance, to print a string with quotation marks, the given code snippet can
be used.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 1/7
10/03/2020 Learn Python 3: Strings Reference Guide | Codecademy
Indexing with negative numbers counts from the end of the string.
str = 'yellow'
str[1] # => 'e'
str[-1] # => 'w'
str[4:6] # => 'ow'
str[:4] # => 'yell'
str[-3:] # => 'low'
Iterate String
To iterate through a string in Python, “for…in” notation is used.
str = "hello"
for c in str:
print(c)
# h
# e
# l
# l
# o
length = len("Hello")
print(length)
# output: 5
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 2/7
10/03/2020 Learn Python 3: Strings Reference Guide | 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.
Immutable strings
Strings are immutable in Python. This means that once a string has been
de ned, it can’t be changed.
There are no mutating methods for strings. This is unlike data types like lists,
which can be modi ed once they are created.
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 would
create an IndexError :
fruit = "Berry"
indx = fruit[6]
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 3/7
10/03/2020 Learn Python 3: Strings Reference Guide | Codecademy
If keywords are speci ed within the placeholders, they are replaced with the
corresponding named arguments to the method.
Lower
In Python, the string method .lower() returns a string with all uppercase
characters converted into lowercase. .lower() does not take any input
arguments.
# welcome to chili's
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 4/7
10/03/2020 Learn Python 3: Strings Reference Guide | Codecademy
The Python string method .title() returns the string in title case. With title
case, the rst character of each word is capitalized while the rest of the
characters are lowercase.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 5/7
10/03/2020 Learn Python 3: Strings Reference Guide | Codecademy
The Python string method .find() returns the index of the rst occurrence of
the string passed as the argument. It returns -1 if no occurrence is found.
String replace
The .replace() method is used to replace the occurence of the rst argument
with the second argument within the string.
The rst argument is the old substring to be replaced, and the second argument
is the new substring that will replace every occurence of the rst one within the
string.
fruit = "Strawberry"
print(fruit.replace('r', 'R'))
# StRawbeRRy
The Python string method .upper() returns the string with all lowercase
characters converted to uppercase.
dinosaur = "T-Rex"
print(dinosaur.upper())
# Prints T-REX in the console.
The .join() method is run on the delimiter and the array of strings to be
concatenated together is passed in as an argument.
# An example of a .join()
x = "-".join(["Codecademy", "is", "awesome"])
# prints "Codecademy-is-awesome"
print(x)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-strings/cheatsheet 7/7