Python Strings

Introduction

A Python string is a sequence of text characters. Strings are among the most commonly used data types in Python, and essential for handling text data.

Python strings are easy to use and in many ways, work like other Python collections data structures, such as lists and tuples, do. They can be considered ordered collections of individual characters. Consequently, when you learn about strings, you also gain familiarity with other Python collections and vice versa.

String Literals

A string literal is a representation of a string you write in Python source code. String literals are created by enclosing text on both sides in single quotes ' or double quotes ".

Example:

single_quote = 'Hello, World!'
double_quote = "Hello, World!"

Single-quoted and double-quoted strings are the same except that you can place a single quote character in a double-quoted string and a double quote character in a single-quoted string. The easiest way to create a string that has double quotes is with single quotes, and the easiest way to create a string that has single quotes is with double quotes.

Example:

single_quotes_inside = "And then the dinosaur said 'Roar!'"
double_quotes_inside = 'And then the dinosaur said "Roar!"'

Printing a String to the Console

Strings can be printed to the console using Python’s built-in print() function. If you give print() a value of any other type, it will convert it to a string and print its string representation. Passing print() multiple values (as a comma-separated list of arguments) prints them all separated by spaces.

Example:

print("Hello")  # printing a string
print(10)       # printing an integer
print(1, "Two", 3)  # printing multiple values

Output:

Hello
10
1 Two 3

The Backslash Escape Character

Escaping a character means telling the programming language to either treat characters that are normally interpreted in a special way as a literal part of the string’s data, or treat characters that are normally interpreted as a literal part of the string data in some special way.

Special characters you’ve already been introduced to in this lesson are " and '. You can’t use a single quote inside a single-quoted string because it causes Python to terminate the string. For example, 'And then the dinosaur said 'Roar!'' is interpreted by Python as 'And then the dinosaur said ' followed by some nonsensical syntax. Yes, you can use a double-quoted string, but it does not help when you want both a single and a double quote character in your string. This is what the escape character is for.

You can use the backslash \ escape character to have the character following it escaped.

Example:

text = 'The dinosaur\'s meal was ready and then he said "Roar!"'
print(text)

Output:

The dinosaur's meal was ready and then he said "Roar!"

In the example above, the single-quoted string has both single quote and double quote characters. The single quote is escaped so that it is used as part of the string’s literal data, as demonstrated by the printout of the word “dinosaur’s”.

Since the backslash itself is a special character, to you use it as a literal character, you must escape it with another backslash that precedes it.

Example:

text = 'This is a backslash character: \\'
print(text)

Output:

This is a backslash character: \

The new line character \n and tab character \t are examples of normally literal characters (‘n’ and ‘t’) that get a different, special meaning when escaped.

Triple Quotes and Multiline Strings

Triple single quotes (''') and triple double quotes (""") allow you to write strings on multiple lines. Python will include new line characters in the strings accordingly.

Example:

triple_single_quote = '''Hello,
World!'''
triple_double_quote = """Hello,
World!"""

The strings in the example above end up containing ‘Hello,\nWorld!’, where \n (an escaped ‘n’) is interpreted by Python as the new line character. If we print the string out, it spans two lines of output.

Example:

print(triple_single_quote)

Output:

Hello,
World!

Creating Empty Strings

An empty string is created with a pair of quotes and nothing between them.

Example:

empty_string = ""

f-Strings

f-strings create string literals with embedded variables or other Python expressions.

To create an f-string, place the letter f (F will also work) before the opening quotation mark and use any Python variables or expressions within the string surrounded by a pair or braces {}.

Example:

fahrenheit = 72
print(f"The temperature in Fahrenheit is {fahrenheit}.")

Output:

The temperature in Fahrenheit is 72.

In the example above, the variable fahrenheit is declared with the temperature of 72 (line 1). It is then used in the print statement’s f-string (line 2).

The next example demonstrates using an f-strings with a more complicated Python expression that converts the temperature to Celsius.

Example:

print(f"The temperature in Celsius is {(5/9)*(fahrenheit - 32)}.")

Output:

The temperature in Celsius is 22.22222222222222.

It’s also possible to format the printout of the expressions within the f-string by adding a colon followed by a format descripctor after the expression, but within the braces. (The ‘f’ of f-strings stands for format.)

Example:

print(f"The temperature in Celsius is {((5/9)*(fahrenheit - 32)):.2f}.")

Output:

The temperature in Celsius is 22.22.

The example above truncates the printed temperature to two digits after the decimal point with the format :.2f.

String Literal Prefixes

String literal prefixes modify the way string literals are interpreted. One example is the f-string, which interprets text within curly braces as formatted Python expressions. These are Python’s other string literal prefix types:

r-strings (Raw string literals):

  • Prefix: r or R
  • Description: Treats backslashes () as literal characters and does not interpret them as escape characters.
  • Example: r"C:\Users\Name"

b-strings (Byte string literals):

  • Prefix: b or B
  • Description: Represents a sequence of bytes. Useful for binary data and text in specific encodings.
  • Example: b"Hello, World!"

u-strings (Unicode string literals):

  • Prefix: u or U
  • Description: Specifies a Unicode string. This prefix is mostly relevant for Python 2 compatibility, as all strings are Unicode by default in Python 3.
  • Example: u"Hello, World!"

String literal prefixes can be combined, giving the string literal combined powers. To do this, place all desired prefixes before the opening quote.

Example:

file_name = 'image1.jpg'
full_path = rf"C:\Users\me\images\{file_name}"
print(full_path)

Output:

C:\Users\me\images\image1.jpg

The example above uses a string literal that is both a raw and formatted string in order to embed file_name and easily use the \ character within a Windows path.

String Operators

Concatenation

You can concatenate strings using the + operator. (The + operator works for other Python collections as well.)

Example:

greeting = "Hello, "
name = "Alice"
message = greeting + name
print(message)

Output:

Hello, Alice

String literals can also be concatenated by placing them next to each other, either separated by spaces or not.

Example:

print('Hello, ' 'Alice')
print('Hello, ''Alice')

Output:

Hello, Alice
Hello, Alice

While this syntax is valid, it’s of little use because in the example above, the single string 'Hello, Alice' would do just fine.

Repetition

The * operator allows you to repeat a string multiple times. (The * is also an operator that works for other Python collections.)

Example:

repeated = "Ha" * 3
print(repeated)

Output:

HaHaHa

String Methods

Python strings come with many built-in string methods for performing various operations. As methods, they are called on string objects using the dot (.) notation, for example, my_string.upper(), which is the upper() method called on the string variable my_string. Those methods can also be called directly on string literals, for example, 'Hello, World!'.upper().

Here are some of the most commonly used methods:

Changing Case

  • lower(): Converts all characters to lowercase.
  • upper(): Converts all characters to uppercase.

Example:

text = "Hello, World!"
print(text.lower())
print(text.upper())

Output:

hello, world!
HELLO, WORLD!

Stripping Whitespace

  • strip(): Removes leading and trailing whitespace.
  • lstrip(): Removes leading whitespace only.
  • rstrip(): Removes trailing whitespace only.

Example:

text = "  Hello, World!  "
print(text.strip())
print(text.lstrip())
print(text.rstrip())

Output:

Hello, World!
Hello, World!  
  Hello, World!

Note that since lstrip() removes whitespaces on the left only, the second line of the output above has two trailing spaces, but they are invisible. You can highlight the second line with your mouse to see those.

Replacing Substrings

  • replace(old, new): Replaces occurrences of old with new.

Example:

text = "Hello, World!"
print(text.replace("World", "Python"))

Output:

Hello, Python!

Splitting and Joining

  • split(delimiter): Splits the string into a list of substrings based on the specified delimiter.
  • join(iterable): Joins elements of an iterable object (an object that represents an ordered collection of items) into a single string with the specified delimiter, i.e. the string placed between the items of iterable. The delimiter is the string this method is called on.

Example:

text = "Hello, World!"
words = text.split(", ")
print(words)

new_text = ", ".join(words)
print(new_text)

Output:

['Hello', 'World!']
Hello, World!

String Immutability

Strings in Python are immutable, meaning they cannot be changed after they are created. Any string methods and operators create and return new strings that are distinct from the original ones.

Example:

text = "Hello"
new_text = text.replace("H", "J")
print(text)      
print(new_text) 

Output:

Hello
Jello

In the example above, new_text is assigned an entirely new string that resides in a different location in memory than the original one in text.

Indexing

Indexing is syntax that allows you to access an individual item in a collection by specifying its numeric position within square brackets []. The position is counted starting with 0 from the left. Since strings are collections of characters, this syntax works for them as well for retrieving individual characters.

Example:

text = "Hello"
print(text[1]) 

Output:

e

Negative indexing allows you to start the position count from the right end of the string going down to the left, starting with -1.

Example:

text = "Hello"
print(text[-1]) 
print(text[-2])

Output:

o
l

Indexing is further discussed in the lesson about lists.

Slicing

Slicing allows you to extract a substring by specifying a range of indices. Like an index, a slice is also specified within square brackets with a start and end index separated by a colon :. The end index specifies one index position beyond the end of the slice.

Omitting a start or end index defaults to the beginning and end of the string respectively.

Example:

text = "Hello, World!"
print(text[0:5])  # Hello
print(text[:5])   # Hello
print(text[7:])   # World!

Output:

Hello
Hello
World!

Slicing is another feature that is supported by many other Python collections and there is much more to it. It is therefore covered by a dedicated lesson about slicing.

String Length

The len() function returns the number of characters in a string.

Example:

text = "Hello"
print(len(text))

Output:

5

String Membership

You can check if a substring exists within a string or not using the in and not in operators, respectively.

Example:

text = "Hello, World!"
print("World" in text)  
print("Python" not in text) 

Example:

True
True   

Summary & Reference for Python Strings

A Python string is a fundamental data type used to represent a sequence of text characters.


Strings can be created using string literals, enclosed within either single quotes ' or double quotes ".

single_quote = 'Hello, World!'
double_quote = "Hello, World!"

Strings can be printed to the console using the built-in print() function. Other data types can be converted to strings for printing by passing them to print().

print("Hello")
print(10)
print(1, "Two", 3)

The backslash (\) is used as an escape character to include special characters within strings, including quotes, the backslash character itself, new line character \n and tab \t.

text = 'The dinosaur\'s meal was ready and then he said "Roar!"'

Triple quotes (''' or """) allow multiline strings, which preserve new line characters within the string.

triple_single_quote = '''Hello,
World!'''
triple_double_quote = """Hello,
World!"""

An empty string is created with a pair of quotes and nothing between them.

empty_string = ""

f-Strings (f"...") provide a convenient way to embed variables or expressions within strings, allowing for dynamic string formatting.

fahrenheit = 72
print(f"The temperature in Celsius is {((5/9)*(fahrenheit - 32)):.2f}.")

In addition to f-strings, the r prefix treats backslashes as literal characters, b creates binary strings, and u unicode strings. These prefixes can be combined to create string literals with combined features.

file_name = 'image1.jpg'
full_path = rf"C:\Users\me\images\{file_name}"

The + string operator concatenates two strings together, and the * repeats a string a specified number of times.

'Hello, ' + 'World!'  # --> 'Hello, World!'
'123' * 3  # --> '123123123'

Strings have various methods that can be called on them using the dot . notation. Those include lower(), upper(), strip(), lstrip(), rstrip(), replace(), split(), and join().

'text'.upper()  # --> 'TEXT'

Individual characters of a string can be accessed with indexing by specifying the position within square brackets [], where 0 is the first position increasing to the right.

text = "Hello"
text[1]  # --> e

Negative indexing allows you to start the position count from the right end of the string going down to the left, starting with -1.

text = "Hello"
text = "Hello"  # --> l

Slicing allows you to extract a substring by specifying a range of indices within square brackets [], with start and end indices separated by a colon :. Omitting a start or end index defaults to the beginning and end of the string respectively.

text = "Hello, World!"
text[0:5]   # --> Hello
text[7:]   # --> World!

The len() function returns the number of characters in a string.

text = "Hello"
len(text)  # --> 5

Strings are immutable in Python, meaning they cannot be changed after creation. Any string methods and operators create and return new strings.


You can check if a substring exists within a string or not using the in and not in operators, respectively.

text = "Hello, World!"
"World" in text  # --> True
"Python" not in text  # --> True