Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Python strings are str values for text. You can write them with quotes, inspect them with indexing and methods, and combine them with values using f-strings or str.format(). A string is immutable, so operations such as changing case return a new value rather than altering the original. For byte-oriented data, Python has a separate type: bytes.

What is a Python string?

A Python string is an instance of the built-in str type. The Python Software Foundation describes strings as “immutable sequences of Unicode code points” in its Python 3.14.7 Built-in Types documentation. In practical terms, a string stores text, and you can retrieve parts of it by position or slice.

str and bytes are distinct types: use str for text and bytes for byte sequences. They are not interchangeable merely because both can contain sequences of values. This distinction matters when working with data that is explicitly byte-oriented rather than human-readable text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do you create a string?

Put text inside matching single or double quotes:

greeting = "Hello, Python"
message = 'It works!'

Both quote styles create strings. Choose the one that makes the contents easiest to read; if the text contains the delimiter, you can choose the other style or escape the delimiter with a backslash.

Escapes and multiline strings

Backslashes introduce escape sequences in ordinary string literals. Triple quotes let a literal span multiple lines:

line = "First linenSecond line"
poem = """A first line
and a second line"""

A raw-string prefix changes how backslashes are interpreted, which can be useful when you want backslashes generally retained. It does not bypass all restrictions imposed by Python’s string-literal grammar. The Python 3.14.8 language reference documents quotes, escapes, raw literals, and multiline forms.

How do you access and change string content?

Indexing retrieves an element by position, and slicing retrieves a portion. For example, text[0] gets the first code point and text[1:4] gets a slice from index 1 up to, but not including, index 4.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "Python"
first = text[0]       # "P"
part = text[1:4]      # "yth"

Strings are immutable: you cannot edit an existing string in place. Methods and operations produce results, leaving the original value unchanged:

name = "Ada"
uppercase = name.upper()
print(uppercase)  # ADA
print(name)       # Ada

Common methods include .upper() and .lower() for case conversion, .strip() for removing surrounding whitespace, and .find() for locating a substring. Each returns a result; none modifies the original string. Indexes and slices work on Unicode code points, so a string’s length is not necessarily the same as the number of characters a person perceives, especially with combining marks or emoji sequences.

How do you insert values into a string?

Use a formatted string literal, commonly called an f-string, to put expressions directly inside a string. Put each expression in braces:

name = "Ada"
message = f"Hello, {name}!"
print(message)  # Hello, Ada!

An f-string evaluates expressions in the context of the literal. Format specifications after a colon can control how a value is presented, such as its width, alignment, padding, precision, or numeric format.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using str.format()

str.format() places replacement fields in a template and supplies their values when calling the method:

count = 3
summary = "Items: {}".format(count)
print(summary)  # Items: 3

Replacement fields can include conversions such as !s, !r, or !a, as well as format specifications. The Python 3.14.8 string-format syntax reference describes the fields and their behavior. The documentation notes that str.format() and the Formatter class share the same format-string syntax, though Formatter subclasses can define their own syntax.

Choosing between f-strings and str.format()

For a short message whose values are available where you write the text, an f-string keeps the expression beside the words it affects. A str.format() template can be useful when you want the template and supplied values expressed separately. The mechanisms are related but distinct; neither is universally preferable for every situation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should you use str or bytes?

Choose str when your program is working with text. Choose bytes when it is working with byte sequences. Python treats these as separate types, so keep the distinction explicit in code instead of treating a string as an ordinary encoded byte array.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.