Strings
Strings are our first example of a Python sequence. In this case, they’re a sequence of characters. Unlike other languages, strings in Python are immutable. You can’t change a string in place, but you can copy parts of strings to another string to get the same effect.
Creating Strings
You can create strings with single quotes ''
, double quotes ""
, or triple quotes """"""
. The interactive interpreter echoes strings with a single quote, but all are treated exactly the same by Python. The print()
function doesn't print the quotes.
These are all the same:
text = 'hello'
text = "hello"
text = """hello"""
print(text)
Triple quotes aren’t very useful for short strings like these. Their most common use is to create multiline strings.
text = """If music be the food of love, play on.
Give me excess of it, that, surfeiting,
The appetite may sicken and so die.
"""
print(text)
Create with str()
You can make a string from another data type by using the str()
function:
str(98.6)
str(1.0e4)
str(True)
Combine by Using +
We can combine strings using +
or even without:
text = "TED talks are highly " + "overrated!"
print(text)
text = "TED talks are highly " "overrated!"
print(text)
Duplicate with *
We use the *
operator to duplicate a string.
me = "Will you marry me?"
her = "Yes!" * 10
print("I said:", me)
print("She said:", her)
Get a Character with []
As mentioned earlier, strings are just a sequence of characters, and in Python, you can access individual items in a sequence using the []
syntax. Note that in Python, as in so many other programming languages, sequence indices start from 0
and not 1
!
letters = 'abcdefghijklmnopqrstuvwxyz'
print("first letter of alphabet is:", letters[0])
print("tenth letter of alphabet is:", letters[9])
Be careful about indices. If you ask for an index out of the range of the sequence, you'll get errors:
text = "batman"
print("the tenth character is:", text[10]) # IndexError: string index out of range
We can use negative indices to access characters from the end of the sequence:
letters = 'abcdefghijklmnopqrstuvwxyz'
print("last letter of alphabet is:", letters[-1])
print("third last letter of alphabet is:", letters[-3])
Get a Substring with a Slice
You can extract a substring (a part of a string) from a string by using a slice. Here's the syntax: [start:end:step]
. You can omit some of these.
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[:]) # will print the whole sequence
print(letters[10:]) # will print the sequence from index 10 forward
print(letters[20:]) # will print the sequence from index 20 forward
print(letters[10:20]) # will print the sequence from index 10 to index 20 (exclusive)
print(letters[-3:]) # will print the last 3 characters
print(letters[-10:]) # will print the last 10 characters
print(letters[18:-3]) # will print the sequence from index 18 to the fourth before the end
print(letters[-6:-3]) # will print the sequence from 6 before the end to 3 before the end
print(letters[::2]) # from start to end, in steps of 2 characters. or every other character
print(letters[19::4]) # from 19 to the end, in steps of 4
Step can be negative:
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[::-1]) # prints the sequence in reverse
Slices are more forgiving of bad offsets than are single-index lookups with []
. A slice offset earlier than the beginning of a string is treated as 0
, and one after the end is treated as -1
.
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[-50:]) # prints the whole sequence
print(letters[:2024]) # prints the whole sequence
Get Length with len()
You can use the len()
built-in function to get the length of a sequence, including a string.
text = "The line must be drawn here! This far, no further!"
print("length of the text is:", len(text))
text = ""
print("length of the text is:", len(text))
Substitute by Using replace()
You use replace()
for simple substring substitution. Give it the old substring, the new one, and how many instances of the old substring to replace. It returns the changed string but does not modify the original string.
review = "The latest Matrix movie was awesome!"
correction = review.replace("awesome", "terrible")
print(correction)
# try printing review
without_karimi_coins = "Alan gets B, Alice gets B+, John gets B, Barbara gets B"
with_karimi_coins = without_karimi_coins.replace("B", "A")
print(with_karimi_coins)
with_karimi_coins = without_karimi_coins.replace("B", "A", 1)
print(with_karimi_coins)
Strip with strip()
strip()
removes leading and trailing whitespace from a string. There's also lstrip()
for removing whitespace from the left of the string and rstrip()
for removing whitespace from the right of the string.
text = " I dare do all that may become a man. "
print(text.strip())
Search with find()
and index()
We can find the offset (index) of a substring using find()
and index()
. If the substring exists, both return its index, otherwise, index()
returns an error, whereas find()
returns -1
.
text = "look there, that's Waldo!"
print(text.find("Waldo"))
print(text.index("Waldo"))
print(text.find("Batman"))
print(text.index("Batman"))
There's also rfind
and rindex
to search from the end of a string.
Count Occurances with count()
You can see how many times a substring is repeated by using count()
.
text = "I am the decider of us three. I decide! Let the decider decide."
print(text.count("decider"))
You can also use the in
operator to see if a substring is in a string. It returns True
if it is, and False
otherwise.
crew = "Superman, Batman, Wonder Woman"
print("Batman" in crew)
print("Flash" in crew)
Change Case
You can use the following methods to change case in a string:
text = "The first rule of Fight Club is: you do not talk about Fight Club."
print(text.lower())
print(text.upper())
print(text.title())
print(text.capitalize())
print(text.swapcase())
Interpolation & Formatting
Interpolation refers to inserting or embedding values or variables within a string. Although there are other ways of interpolating/formatting a string, since Python 3.6, the recommended way is using f-strings. Here's how to use them:
Type the letter f or F directly before the initial quote.
- Type the letter
f
orF
directly before the initial quote. - Include variable names or expressions within curly brackets (
{}
) to get their values into the string.
adjective_1 = "mindful"
adjective_2 = "demure"
text = f"Very {adjective_1}, very {adjective_2}."
print(text)
You can interpolate any value that's printable. It doesn't have to be a string:
tiktok_followers = 3010
insta_followers = 5618
print(f"I have {tiktok_followers} followers on TikTok & {insta_followers} followers on Instagram")
print(f"1 + 2 = {1+2}") # you can use expressions too
You can also format strings using f-strings. The general rule is: :[alignment][width][,][.precision][type]
:
number = 50429.5
print(f"{number:.2f}") # precision & type
print(f"Number is {number:10.2f}.") # width, precision, & type
print(f"Number is {number:<10.2f}.") # alignment, width, precision, & type
print(f"Number is {number:<10,.2f}.") # alignment, width, thousand separator, precision, & type
print(f"Number is {number:e}.") # scientific notation (type)
hours, minutes, seconds = 10, 5, 9 # multiple assignments
print(f"{hours:02d}:{minutes:02d}:{seconds:02d}") # padding with 0s
Starting in Python 3.8, f-strings gain a new shortcut that’s helpful when you want to print variable names as well as their values. This is handy when debugging. The trick is to have a single =
after the name in the {}
-enclosed part of the f-string:
number = 9
largest = 42
print(f"{number=}, {largest=}")
More String Things
Python has many more string functions. Check them out here.