English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python Strings (String)

In this tutorial, you will learn how to create, format, modify, and delete strings in Python. You will also be introduced to various string operations and functions.

What are strings in Python?

Strings are sequences of characters.

A character is just a symbol. For example, English has26characters.

Computers do not process characters, they process numbers (binary). Even if you may see characters on the screen, internally they are stored and manipulated as 0s and1.

This kind of conversion from characters to numbers is called encoding, and the opposite process is decoding. ASCII and Unicode are some commonly used encodings.

In Python, strings are sequences of Unicode characters. The introduction of Unicode includes all characters from every language and brings a unified encoding. You can learn more from hereLearn more about Unicode.

How do you create strings in Python?

Strings can be created by enclosing characters in single quotes or double quotes. Python can even use triple quotes, but they are usually used to represent multi-line strings and documentation strings.

# The following are equivalent
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
# Triple-quoted strings can span multiple lines
my_string = "Hello, welcome to
           the world of Python"
print(my_string)

When running the program, the output is:

Hello
Hello
Hello
Hello, welcome to
           the world of Python

How do you access characters in a string?

We can access individual characters using indices and use slicing to access a series of characters. Indices start from 0. Attempting to access characters beyond the index range will raise an IndexError. Indices must be integers. We cannot use floats or other types, as this will cause a TypeError.

Python allows negative indexing for its sequences.

index-1represents the last item-2represents the second-to-last item, and so on. We can use the slice operator (colon) to access a series of items in a string.

str = 'w'3codebox.com'
print('str = ', str)
# The first character
print('str[0] = ', str[0])
# The last character
print('str[-1] = '', str[-1)
# Slicing the second to the fifth characters
print('str[1:5] = '', str[1:5)
# Slicing starts from the6to the last2a character
print('str[5:-2] = '', str[5:-2)

Output result:

str = 'w'3codebox.com
str[0] = 'n'
str[-1] = 'm'
str[1:5] = 'hooo'
str[5:-2] = ''

An error will occur if you try to access an index out of range or use a decimal number.

# Index must be within range
>>> my_string[15]  
>>> print('He said, \"What\'s there?\"')
IndexError: string index out of range
# Index must be an integer
>>> my_string[1.5] 
>>> print('He said, \"What\'s there?\"')
TypeError: string indices must be integers

By considering the index is between elements, it is best to visualize slices as shown below.

To access a range, you need an index, which will cut out a part of the string.

How to change or delete a string?

Strings are immutable. This means that once the elements of a string are assigned, they cannot be changed. We can simply reassign different strings to the same name.

>>> my_string = 'w'3codebox.com'
>>> my_string[5] = 'a'
>>> print('He said, \"What\'s there?\"')
TypeError: 'str' object does not support item assignment
>>> my_string = 'Python'
>>> my_string
'Python'

We cannot delete or delete characters from a string. However, we can completely delete a string using the del keyword.

>>> del my_string[1]
>>> print('He said, \"What\'s there?\"')
TypeError: 'str' object doesn't support item deletion
>>> del my_string
>>> my_string
>>> print('He said, \"What\'s there?\"')
NameError: name 'my_string' is not defined

Python string operations

Strings can perform many operations, making themIn PythonThe most commonly usedOne of the data types.

Concatenation of two or more strings

Connecting two or more strings into a single string is called concatenation.

+ The operator performs concatenation operations in Python. Simply writing two string literals together, or concatenating them together.

* The operator can be used to repeat a string a specific number of times.

str1 = 'Hello'
str2 ='World!"
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)

Writing two string literals together will also be like+The operators can be used to concatenate them together.

If you want to concatenate strings on different lines, you can use parentheses.

>>> # Two string literals together
>>> 'Hello ''World!"
'Hello World!"
>>> # Use parentheses
>>> s = ('Hello '
...        'World')
>>> s
'Hello World'

traverse a string

usingfor loop,We can traverse a string. This is an example of counting the number of 'l' in a string.

count = 0
for letter in 'Hello World':
    if(letter == 'l'):
        count += 1
print(count, 'letters found')

String membership test

We can use the in keyword to test if a substring exists in a string.

>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False

Use Python's built-in functions

You can use various built-in functions of sequence and string.

Some commonly used ones are enumerate() and len(). The enumerate() function is used to: return an enumeration object. It contains pairs of the index and value of all items in the string. This is very useful for iteration.

Similarly, len() returns the length of the string (number of characters).

str = 'cold'
# enumerate()
list_enumerate = list(enumerate(str))
print('list(enumerate(str) = ', list_enumerate)
# Number of characters
print('len(str) = ', len(str))

Python string formatting

Escape sequences

If we want to print a text-He said: "What's there?"-We cannot use single quotes or double quotes. This will cause a SyntaxError because the text itself contains single and double quotes.

>>> print("He said, \"What\'s there?\"")
>>> print('He said, \"What\'s there?\"')
...
>>> print("He said, \"What\'s there?\"")
>>> print('He said, \"What\'s there?\"')
...

SyntaxError: invalid syntax

One way to solve this problem is to use three quotes. Additionally, we can use escape sequences.

Escape sequences start with a backslash and are interpreted in different ways. If we use single quotes to represent a string, we must escape all single quotes within the string. The same goes for double quotes. This is the way to represent the above text.
# Use three single quotes
# Escape single quote
print('He said, \"What\'s there?\"')
# Escape double quotes
print("He said, \"What's there?\"")

This is a list of all the escape sequences supported by Python.

Escape sequences in Python
Escape sequencesDescription
\newlinebackslash and newline are ignored
\\backslash
\'single quotes
\"double quotes
\aASCII bell
\bASCII backspace
\fASCII form feed
\nASCII newline
\rASCII carriage return
\tASCII horizontal tab
\vASCII vertical tab
\oooof characters with octal value ooo
\xHHof characters with hexadecimal value HH

Here are some examples

>>> print("C:\\Python32\\Lib)
C:\\Python32\Lib
>>> print("This is printed\nin two lines")
This is printed
in two lines
>>> print("This is \x48\x45\x58 representation)
This is the HEX representation

Raw strings ignore escape sequences

Sometimes we may want to ignore escape sequences in strings. To do this, we can place them before the r or R at the beginning of the string. This means it is a raw string and any escape sequence will be ignored.

>>> print("This is \x61 This is a good example))
This is a
good example
>>> print(r"This is \x61 This is a good example))
This is \x61 This is a good example

Formatting strings with the format() method

The format() method used with string objects is very general and very powerful in string formatting. The format string contains braces {} as placeholders or replacement fields.

We can use positional arguments or keyword arguments to specify the order.

# Default (implicit) order
default_order = "{}, {} and {}".format('John','Bill','Sean')
print('\n--- Default Order ---)
print(default_order)
# Sorting using positional arguments
positional_order = "{1, {0} and {2}".format('John','Bill','Sean')
print('\n--- Positional Order ---)
print(positional_order)
# Sorting using keyword arguments
keyword_order = "{s}, {b} and {j}".format(j='John',b='Bill',s='Sean')
print('\n--- Keyword Order ---)
print(keyword_order)

The format() method can have optional format specifications. They use colons to separate field names. For example, we can left-align with <, right-align with >, or center the string with ^ in the given space. We can also format integers as binary, hexadecimal, etc., and floating-point numbers can be rounded or displayed in exponential format. You can use a large number of formats. Please visit here toget the format()methodsavailableAllString formatting.

>>> # Formatting integers
>>> "Binary representation of {0} is {0:b}".format(12)
'Binary representation of 12 is 1100'
>>> # Formatting floating-point numbers
>>> "Exponent representation: {0:e}".format(1566.345)
'Exponent representation: 1.566345e+03"
>>> # Rounding
>>> "One third is: {0:.3f}".format(1/3)
'One third is: 0.333"
>>> # String alignment
>>> "|{:<10}|{:^10}|{:}>10}|".format('butter','bread','ham')
'|butter    |    bread    |    ham|'

Old style format

We can even format strings in the old style like sprintf() in C programming language. We use the % operator to complete this task.

>>> x = 12.3456789
>>> print('The value of x is %',3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %',3.4f' %x)
The value of x is 12.3457

Common Python string methods

String objects have many available methods. The format() method mentioned above is one of them. Common methods include lower(), upper(), join(), split(), find(), replace(), and so on. Here is a complete list of themBuilt-in methods for string processing in Python.

>>> 'w3codebox.lower()
'w3codebox
>>> 'w3codebox.upper()
'w3codebox
>>> 'This will split all words into a list'.split()
['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
>>> ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
'This will join all words into a string'
>>> 'Happy New Year'.find('ew')
7
>>> 'Happy New Year'.replace('Happy','Brilliant')
'Brilliant New Year'