Python Strings
A string in Python is a series of characters enclosed into quotes or double quotes:
"Roger"
'Roger'
You can assign a string value to a variable:
name = "Roger"
You can concatenate two strings using the +
operator:
phrase = "Roger" + " is a good dog"
You can append to a string using +=
:
name = "Roger"
name += " is a good dog"
print(name) #Roger is a good dog
You can convert a number to a string using the str
class constructor:
str(8) #"8"
This is essential to concatenate a number to a string:
print("Roger is " + str(8) + " years old") #Roger is 8 years old
A string can be multi-line when defined with a special syntax, enclosing the string in a set of 3 quotes:
print("""Roger is
8
years old
""")
#double quotes, or single quotes
print('''
Roger is
8
years old
''')
A string has a set of built-in methods, like:
isalpha()
to check if a string contains only characters and is not emptyisalnum()
to check if a string contains characters or digits and is not emptyisdecimal()
to check if a string contains digits and is not emptylower()
to get a lowercase version of a stringislower()
to check if a string is lowercaseupper()
to get an uppercase version of a stringisupper()
to check if a string is uppercasetitle()
to get a capitalized version of a stringstartsswith()
to check if the string starts with a specific substringendswith()
to check if the string ends with a specific substringreplace()
to replace a part of a stringsplit()
to split a string on a specific character separatorstrip()
to trim the whitespace from a stringjoin()
to append new letters to a stringfind()
to find the position of a substring
and many more.
None of those methods alter the original string. They return a new, modified string instead. For example:
name = "Roger"
print(name.lower()) #"roger"
print(name) #"Roger"
You can use some global functions to work with strings, too.
In particular I think of len()
, which gives you the length of a string:
name = "Roger"
print(len(name)) #5
The in
operator lets you check if a string contains a substring:
name = "Roger"
print("ger" in name) #True
Escaping is a way to add special characters into a string.
For example, how do you add a double quote into a string that’s wrapped into double quotes?
name = "Roger"
"Ro"Ger"
will not work, as Python will think the string ends at "Ro"
.
The way to go is to escape the double quote inside the string, with the \
backslash character:
name = "Ro\"ger"
This applies to single quotes too \'
, and for special formatting characters like \t
for tab, \n
for new line and \\
for the backslash.
Given a string, you can get its characters using square brackets to get a specific item, given its index, starting from 0:
name = "Roger"
name[0] #'R'
name[1] #'o'
name[2] #'g'
Using a negative number will start counting from the end:
name = "Roger"
name[-1] #"r"
You can also use a range, using what we call slicing:
name = "Roger"
name[0:2] #"Ro"
name[:2] #"Ro"
name[2:] #"ger"
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025