Python Strings (with Examples)

Strings in Python are stored as a str data type. A Python string is a sequence of characters enclosed in single or double quotes.

  • Single-quote: 'hello'
  • Double-quote: "hello"
Navigation Show

What is a Python String

A String, in Python Programming, is represented by the string class “str“. String literals represent a sequence of characters (string value) enclosed in quotes inside a Python program.

The University of California, Berkley mentions that the Python string type are inherently in the Unicode Standard, composed of 109K characters, which means they can represent characters beyond the ASCII range of 128 characters.


Subscribe to my Newsletter


According to the Python foundation, Python strings are immutable (cannot be changed after creation). Since they can’t be changed, we generally use string construction to create new strings. Strings in Python allow escaping special characters with a backslash. The escape characters, or escape sequences, represent reserved characters in strings, such as the line break for instance (e.g. \n in 'This is a \nline break' ).

Based on the UCSB College of Engineering, Python strings allow simple indexing, slicing, assignment and formatting, while providing built-in string operation methods to operate on the string object.

A Python string data type (str) should not be confused with the string module in Python which provides additional constants and classes for string-related operations. Google for Education program even mentions that you should not to use the string module.

Characteristics of Python Strings

Python strings have multiple characteristics:

  • Immutable: String cannot be changed after creation.
  • Sequence Type: Represents sequences of characters with indexing.
  • Must be Enclosed in Quotes: Single, double, or triple quotes.
  • Supports Unicode characters: Character encoding standard
  • Allows Escaping Special Characters.
  • Supports sequence operations
  • Variable Length

Examples of Python Strings

Examples of Python strings vary with quotes, lines, nesting, and variable use. Here are a few examples of Python strings and string initialization:

  • Single-quote strings: 'single'
  • Double-quote strings: "double"
  • Triple-quote strings: '''triple single-quote''' or """triple double-quote"""
  • F-strings: f"my name is {name}, {last_name}"
  • Format Strings: "my name is {0}, {1}".format("JC","Chouinard")
  • % string: "My name is %s, %s and I am %d years old" % ('JC', 'Chouinard', 33)
  • Raw Strings: r'Raw string with backslashes treated literally: \n \t'
  • Byte Strings: b'Byte string of binary data: \x48\x65\x6c\x6c\x6f'

Print a Python String

To print a string in Python, pass the string as an argument of the print() function.

print('hello')
print("hello")

Assign a String to Variable

Use the equal operator (=) to assign a string to a Python variable.

# String Assignment
var = 'hello'
var

Multiline Strings in Python

Multiline strings can be done with the single-quote or the double-quote notation. We need to triple the number of quotes in order to make multi-line strings.

# Use triple quotes to assign multiline
x = '''
Hello, 
I am 
JC
'''
print(x)
# Can also use double-quotes
x = """
Hello, 
I am 
JC
"""
print(x)

Nested Strings in Python

Nested strings contain quote symbols (‘ or “) in Python. Nested strings can happen when a quotation is encapsulated inside a string.

How to Double-Nest Strings in Python

In order to create nested strings in Python you need to use different quotation symbol for the quotation than the one used to define the string. (e.g. using single-quote inside the double-quote).

Although it is possible to use double-quotes inside single quotes, the standard is to use single-quotes inside double-quotes. The important is to be consistent.

# Nested Strings
x = """
My friend said: 
'hey, listen to me'
"""
print(x)

How to Triple-Nest Strings in Python

The preferred format for nested strings is to have double quotes > single quote > backtick with the backtick being the most nested and the double quote being the outer string assignment.

# Triple Nest
x = """
My friend said: 
'hey, listen to what this guy told me. He said:
`it is possible to triple-nest strings in Python!`.
What a dumb idea...'.
What a jerk!
"""
print(x)

Indexing of Strings

A string in Python is an Array, which allows for string indexing.

An array in Python is when you can have multiple items in a single variable.

Thus, it is possible to select each item of the array (string) individually using its index.

Selecting First Letter of a String

Python is using zero-based indexing (starts at 0).

# Selecting first letter of a string
x = 'python'
print(x[0])

Selecting Last Letter of a String

To select the last letter of a string, we use the minus symbol.

# Selecting last letter of a string
x = 'python'
print(x[-1])

Looping Through a String in Python

For loops can be used to loop through any types of arrays, including strings.

# looping
for i in 'python':
    print(i)

Slicing Strings in Python

Slicing a string in Python is when you want to select a part of the string array (e.g. the substring).

How to Get a Part of a String Using the Indexes

It is possible to use the slice syntax to get a sub-part of the string sequence (string slice). String slicing is done by calling the string with square bracket (s[start:end]) where elements included begin at start, up to, but not including, the end.

# Get part of string
x = 'Slice me like a cake'
print(x[1:4])
# 4 is excluded
lic

How to Slice a String from the Start

To slice a string until an index, use the colon notation up to an position. The position chosen is excluded.

In the example below, I am slicing from index 0 until the index 9, 9 excluded.

Adding no number before the colon is implied that you are starting from index 0.

# Slice from the start
x = 'Slice me like a cake'
print(x[:9])
Slice me 

How to Slice String from Index to the End

Similarly, adding no number after the colon makes it implicit that you want to slice until the end.

This time, the number 9 (index before the colon) is included.

# Slice from index to the end
x = 'Slice me like a cake'
print(x[9:])
like a cake

Negative Slicing of Strings

# Negative slicing
x = 'Slice me like a cake'
print(x[:-1])
Slice me like a cak

Find How Long a String is in Python

Use the len() function to find how long a string is in Python.

# How long is a string
x = 'How long is this string'
len(x)

How to Count Letters in a String

# String to variable
x = 'How long is this string'

# count the number of 's' in the string
x.count('s')

Check if a String Contains Another String

# String to variable
x = 'How long is this string'

# Does the string contain the word "long"
if 'long' in x.lower():
    print('yes, it is') 
else:
    print('no, it is not')

Change the Casing of Strings in Python

We can modify strings in many ways in Python. One of such ways is to modify casing of the string.

How to Convert a String to Uppercase

# Lower Case
x = 'Convert casing of this string'

# Uppercase
new_x = x.upper()

print(new_x)
CONVERT CASING OF THIS STRING

How to Convert a String to Lowercase

# Lower Case
x = 'Convert casing of this string'

# Lowercase
new_x = x.lower()

print(new_x)
convert casing of this string

How to Convert a String to Titlecase

# Title Case
x = 'Convert casing of this string'

# Titlecase
new_x = x.title()

print(new_x)
Convert Casing Of This String

Remove Whitespaces in Strings

Remove Spaces Before and After a String (Leading and Trailing)

The strip() method removes leading and trailing spaces in strings, but not the spaces between the words.

# Remove Spaces Before and after
x = '   Replace leading and trailing spaces from this string    '

print(x.strip())
Replace leading and trailing spaces from this string

Remove all Whitespaces in a String

Remove whitespaces using the replace() method.

# Remove all whitespaces
x = '   Replace whitespaces from this string    '

x.replace(' ', '')
Replacewhitespacesfromthisstring

Replacing Letters from Strings

How to Replace a Single Letter inside a String

# Replace the letter e
x = 'Replace the letter e'

x.replace('e', 'f')
Rfplacf thf lfttfr f

How to Replace the First Occurrence of a Letter in String

# Replace the letter e
x = 'Replace the letter e'

x.replace('e', 'f', 1)
Rfplace the letter e

Replace Letters Using a Dictionary

Example with a simple Password encryption.

# Replace letters
encrypt = {
    'a':'b',
    's':'g',
    'o':'0',
    'p':'v',
    'w':'#',
    'd':'n'
}

x = 'password'

for k,v in encrypt.items():
    x = x.replace(k, v)

x
vbgg#0rn

Decrypt Password

for k,v in encrypt.items():
    x = x.replace(v, k)

x
'password'

Splitting and Joining Strings in Python

How to Split a String in Python

The split() method allows you to split a string, using the character where you want to split the string as an argument.

# Split
x = 'hello, world'
splitx = x.split(',')
splitx
['hello', ' world']

How to Join a String in Python

The join() method can be used to join a list of items into a single string. The argument of the join() method is an array.

# Join
','.join(splitx)
'hello, world'

Concatenate Strings in Python

Concatenating strings is the act of joining strings together. We have seen the join() method

How to Combine String Variables in Python

To combine string variables in Python, use string concatenation.

# Combine string variables
x = 'Jean-Christophe'
y = 'Chouinard'

x + y
'Jean-ChristopheChouinard'

How to Add a Space in String Concatenation

# Add space in Concatenate
x + ' ' + y
'Jean-Christophe Chouinard'

What is String Interpolation?

String interpolation is used to construct strings by embedding expressions or variables within string literals. Simply put, it is used embed values or expressions directly into a string, making the code more readable and concise. There are 3 main ways to perform string interpolation in Python:

  • F-strings: Formatted string literal
  • str.format() method: Alternative string formatting operation
  • % string: Not recommended

F-Strings in Python

F-Strings, or formatted string literals, are a string type that uses string interpolation to embed operations or variables inside Python strings.

They are strings prefixed with 'f' that allow you to convert Python objects into a printable string using expressions inside curly braces {}.

How to Add variable to a Python String

# Add variable to string
name = 'JC'
age = 12

s = f'Hello, I am {name} and I am {age}!'

print(s)
Hello, I am JC and I am 12!

Counting Inside an F-String

# Count
name = 'JC'
age = 12

s = f'Hello, I am {name} and I am {age * 3}!'

print(s)
Hello, I am JC and I am 36!

Applying Methods to an F-String

# Apply methods
name = 'JC'
age = 12

s = f'Hello, I am {name.lower()} and I am {age}!'

print(s)
Hello, I am jc and I am 12!

How to Use str.format() in Python

The str.format() method can be used in Python format strings by converting objects into a string. According to Python.org’s Format String syntax documentation, the format() method uses format specifications within replacement fields (expression inside the curly braces {}) to define the objects and the literal text (outside curly braces).

print("My name is {0}, {1}".format("JC","Chouinard"))
My name is JC, Chouinard

How to Use String % Formatting

To use the %-formatting operation in Python, add the printf-type format string on the left (e.g. %d int, %s string, %f floating point) of a percentage sign (%) and match values to the right within a Tuple.

The official Python foundation documentation recommends using f-strings over % string formatting for improved readability and error handling.

That being said, here is how to use String %-formatting in Python.

name = 'JC'
"Hello, %s" % name
'Hello, JC'
name = 'JC'
last_name = 'Chouinard'
age = 32
born_in = 1990
"Hello, %s %s. You are %s years old, born in %s " % (name, last_name, age,born_in)

How to Perform String Comparison in Python

String comparison in Python is performed using operators

str1 = "car"
str2 = "plane"

# Using comparison operators
if str1 == str2:
    print("Both strings are equal.")
elif str1 < str2:
    print(f"{str1} comes before {str2}.")
else:
    print(f"{str2} comes before {str1}.")
car comes before plane.

Boolean Test on Strings

We can perform boolean test to check the truth value on a string. For example, we can verify whether a string is empty using the == operator.

my_string = ""
(my_string == "")
True

How to Encode and Decode Python Strings

To encode and decode Python strings, use the encode() and decode() methods on the string object, and pass the relevant encoding as an argument. By default, strings in Python are encoded using your system default encoding, which generally is 'utf-8'.

Unicode Strings vs bytes

Unicode is the standard of regular Python strings. However, Python also supports bytes strings where each character represents a byte of data, and the values can range from 0 to 255.

Encoding a string serves the purpose of converting a string to bytes that can be decoded later.

# Original string
original_string = "Hello, ????!"  # Includes a non-ASCII character

# Encoding the string to UTF-8
encoded_bytes = original_string.encode("utf-8")

# Display the encoded bytes
print("Encoded Bytes:", encoded_bytes)

# Decoding the bytes back to a string
decoded_string = encoded_bytes.decode("utf-8")

# Display the decoded string
print("Decoded String:", decoded_string)
Encoded Bytes: b'Hello, \xf0\x9f\x8c\x8d!'
Decoded String: Hello, ????!

Python Strings Methods

Here are the most useful Python string methods.

capitalize()Converts the first character to upper case
count()Count occurrences of a value in string
encode()Returns encoded version of a string
startswith()Returns True if string starts with a value
endswith()Returns True if string ends with a value
find()Returns position of a value if found in string
isalnum()Returns True if all characters in the string are alphanumeric
isalpha()Returns True if all characters in the string are in the alphabet
isnumeric()Returns True if all characters in the string are numeric
islower()Returns True if all characters in the string are lower case
join()Converts the elements of an iterable into a string
lower()Converts a string into lower case
replace('start','end')Returns a string where a specified value is replaced with a specified value
split()Splits the string at the specified separator, and returns a list
strip()Returns a trimmed version of the string
title()Converts the first character of each word to upper case
upper()Converts a string into upper case
isspace()Returns True if all characters in the string are whitespaces.

Difference Between Python String and the Python String module

The term “Python String” can cause confusion between the built-in data type str in Python and the Python string module in Python’s standard library. A “Python string” represents a sequence of characters and the “Python String module” represents the string module that provides additional constants and classes related to string manipulations.

Parsing Strings with Regular Expressions

Regular Expressions (RegEx) can be used to parse Python strings and find patterns in text.

import re

sentence = "We have products ranging from $199.99 to $299.99."

# Regular expression to match prices in dollars and cents
price_pattern = r'\$\d+\.\d{2}'

# Using regular expression to find all prices in the sentence
prices = re.findall(price_pattern, sentence)

# Display the extracted prices
print("Extracted Prices:", prices)
Extracted Prices: ['$199.99', '$299.99']

This was a very simple introduction to Python strings, I strongly suggest that you dive deeper into F-Strings.

Enjoyed This Post?