CS101_ Introduction to Programming_ Python_ Strings Cheatsheet _ Codecademy
CS101_ Introduction to Programming_ Python_ Strings Cheatsheet _ Codecademy
Python: Strings
The Python string method .format() replaces empty msg1 = 'Fred scored {} out of {} points.'
brace ( {} ) placeholders in the string with its
msg1.format(3, 10)
arguments.
If keywords are specified within the placeholders, they # => 'Fred scored 3 out of 10 points.'
are replaced with the corresponding named arguments
to the method.
msg2 = 'Fred {verb} a {adjective}
{noun}.'
msg2.format(adjective='fluffy',
verb='tickled', noun='hamster')
# => 'Fred tickled a fluffy hamster.'
The string method .lower() returns a string with all greeting = "Welcome To Chili's"
uppercase characters converted into lowercase.
print(greeting.lower())
# Prints: welcome to chili's
String Method .strip()
The string method .strip() can be used to remove text1 = ' apples and oranges '
characters from the beginning and end of a string.
text1.strip() # => 'apples and
A string argument can be passed to the method,
specifying the set of characters to be stripped. With no oranges'
arguments to the method, whitespace is removed.
The string method .title() returns the string in title my_var = "dark knight"
case. With title case, the first character of each word is
print(my_var.title())
capitalized while the rest of the characters are
lowercase.
# Prints: Dark Knight
The string method .split() splits a string into a list of text = "Silicon Valley"
items:
If no argument is passed, the default behavior is
to split on whitespace. print(text.split())
If an argument is passed to the method, that # Prints: ['Silicon', 'Valley']
value is used as the delimiter on which to split
the string.
print(text.split('i'))
# Prints: ['S', 'l', 'con Valley']
Python string method .find()
The Python string method .find() returns the index of mountain_name = "Mount Kilimanjaro"
the first occurrence of the string passed as the
print(mountain_name.find("o")) # Prints 1
argument. It returns -1 if no occurrence is found.
in the console.
String replace
The string method .upper() returns the string with all dinosaur = "T-Rex"
lowercase characters converted to uppercase.
print(dinosaur.upper())
# Prints: T-REX
Strings
Backslashes ( \ ) are used to escape characters in a txt = "She said \"Never let go\"."
Python string.
print(txt) # She said "Never let go".
For instance, to print a string with quotation marks, the
given code snippet can be used.
The in Syntax
The in syntax is used to determine if a letter or a game = "Popular Nintendo Game: Mario
substring exists in a string. It returns True if a match is
Kart"
found, otherwise False is returned.
Python strings can be indexed using the same notation str = 'yellow'
as lists, since strings are lists of characters. A single
str[1] # => 'e'
character can be accessed with bracket notation
( [index] ), or a substring can be accessed using slicing str[-1] # => 'w'
( [start:end] ). str[4:6] # => 'ow'
Indexing with negative numbers counts from the end of
str[:4] # => 'yell'
the string.
str[-3:] # => 'low'
Iterate String
# h
# e
# l
# l
# o
Built-in Function len()
String Concatenation
To combine the content of two strings into a single x = 'One fish, '
string, Python provides the + operator. This process
y = 'two fish.'
of joining strings is called concatenation.
z = x + y
print(z)
# Output: One fish, two fish.
Immutable strings
IndexError