Skip to content

Python String

How to create, manipulate, and format strings in Python, including indexing, slicing, concatenation, escape sequences, and common string methods like lower(), upper(), and replace().

A Python string is a sequence of characters and a character is simply a symbol.

Computers internally handle strings through sequences of bits.

To convert the bits into characters that people can understand, a conversion process is carried out using a certain encoding.

The most popular standard encodings are ASCII and Unicode.

In Python, a string is a sequence of Unicode characters.

Create Python String

Strings can be created by enclosing characters within single or double quotation marks.

Triple quotes can also be used in Python, but they are typically used to represent multiline strings and docstrings.

my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

my_string = '''Hello'''
print(my_string)

my_string = """Hello, this is 
a multi-line string"""
print(my_string)

Access characters in a string

Individual characters can be accessed using indexing and ranges of characters using slicing.

with Index

  • In Python, the index starts from 0.
  • The index must be an integer. Using values of any other type will result in a TypeError.
s = 'Tutorial Reference'
print('s = ', s) # s = Tutorial Reference

print('s[0] = ', s[0]) # s[0] =  T
print('s[1] = ', s[1]) # s[1] =  u
print('s[5] = ', s[5]) # s[5] =  i

with Negative Index

Python allows negative indexing for its sequences.

s = 'Tutorial Reference'
print('s = ', s) # s = Tutorial Reference

print('s[-1] = ', s[-1]) # s[-1] =  e
print('s[-2] = ', s[-2]) # s[-2] =  c
print('s[-5] = ', s[-5]) # s[-5] =  r

Slicing

A range of elements in a string can be accessed using the slicing operator :.

The possible cases (with examples) are shown in the table below:

Slice Description Example with s="abcd"
my_str[from:to] the elements from from to to-1 s[1:3] -> "bc"
my_str[from:] the elements from from to the end of the string s[2:] -> "cd"
my_str[:to] the elements from the beginning of the list to the element to-1 li[:3] -> "abc"
my_str[:] all elements s[:] -> "abcd"
my_str[from:to:step] one element each step from from to to-1 s[0:3:2] -> "ac"
my_str[from::step] one item each step from from until the end of the string s[1::2] -> "bd"
my_str[:to:step] one element each step from the beginning to the to-1 element. s[:3:2] -> "ac"
my_str[::step] one item each step s[::2] -> "ac"

Python String Manipulation and Operations

Strings are immutable, i.e., elements of a string can't be changed once they have been assigned.

Change a string

You can simply reassign different strings to the same name.

my_str = 'Tutorial Referece'


my_str = 'Other String'
print(my_str) # my_str 

Delete a string

Characters can't be deleted or removed from a string because strings are immutable.

However, it is possible to completely delete a string using the del statement

my_str = "a string"
print(my_str) # a string


del my_str # delete my_str

Concatenation of Two or More Strings

Concatenation is the joining of two or more strings into one string

  • In Python the + operator performs concatenation.
  • The * operator can be used to repeat the string a specified number of times.
str1 = 'Hello'
str2 ='World'

print(str1 + str2) # HelloWorld

print(str1 * 3) # HelloHelloHello

Also, writing two string literals together concatenates them, as does the + operator.

my_str = 'Tutorial ''Reference'
print(my_str) # Tutorial Reference

s = ('Tutorial '
    'Reference')
print(s) # Tutorial Reference

Iterating Through a string

Using a for loop, it is possible to iterate each character of a string.

count = 0
for letter in 'Tutorial Reference':
    if(letter == 'e'):
        count += 1
print(count,'letters found')

Output

4 letters found

String Membership Test

The keyword in is used to check whether a character exists in a string.

my_str= 'Tutorial Reference'
 
print('t' in my_tuple) # True
print('h' in my_tuple) # False
print('q' not in my_tuple) # True

Built-in functions to Work with Python

Some of the commonly used built-in functions are enumerate() and len().

  • The enumerate() function returns an enumerate object. It contains the index and value of all the elements of the string as pairs. This can be useful for iteration.
  • len() function returns the length (i.e., the number of characters) of the string.
my_str = 'Tutorial'

list_enumerate = list(enumerate(my_str))
print('list(enumerate(my_str) = ', list_enumerate)

print('len(my_str) = ', len(my_str ))

Output

list(enumerate(my_str) =  [(0, 'T'), (1, 'u'), (2, 't'), (3, 'o'), (4, 'r'), (5, 'i'), (6, 'a'), (7, 'l')]
len(my_str) =  8

Python String Formatting

Escape Sequence

Consider this string: He said, "What's there?". This will result in a SyntaxError as the text itself contains both single and double quotes.

One way around this problem is to use triple quotes. Alternatively, escape sequences can be used.

An escape sequence begins with a backslash and it is interpreted.

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

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

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

Output:

He said, "What's there?"
He said, "What's there?"
He said, "What's there?"

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

Escape Sequence Description
\newline Backslash and newline ignored
\\ Backslash
\' Single quote
\" Double quote
\a ASCII Bell
\b ASCII Backspace
\f ASCII Formfeed
\n ASCII Linefeed
\r ASCII Carriage Return
\t ASCII Horizontal Tab
\v ASCII Vertical Tab
\ooo Character with octal value ooo
\xHH Character with hexadecimal value HH

For example:

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

Raw String to ignore escape sequence

To ignore escape sequences within a string, r or R must be placed in front of the string.

This implies that it is a raw string and any escape sequence within it will be ignored.

For example:

print("This is \x61 \ngood example")
print(r"This is \x61 \ngood example")

Output

This is a
good example
This is \x61 \ngood example

The format() Method for Formatting Strings

The format() method is used for formatting strings. Format strings contain curly braces {} as placeholders or replacement fields which get replaced.

default_order = "{}, {} and {}".format('Tom','Ryan','John')
print(default_order) # Tom, Ryan and John

positional_order = "{1}, {0} and {2}".format('Tom','Ryan','John')
print(positional_order) # Ryan, Tom and John

keyword_order = "{t}, {r} and {j}".format(t='Tom',r='Ryan',j='John')
print(keyword_order) # Tom, Ryan and John

Old style formatting wit % operator

Using the % operator, strings can be formatted (like sprintf() in C language).

x = 123.456789
print('x = %3.2f' %x) # x = 123.46
print('x = %3.4f' %x) # x = 123.4568

The format() method has optional format specifications. For example is possible to left-justify <, right-justify > or center ^ a string in the given space.

Other Common String Operations

There are numerous methods available with the string object.

Some of the commonly used methods are format() (discussed above), lower(), upper(), join(), split(), find(), replace() etc.

my_str = "TuTOriAl RefERenCe"

print(my_str.lower())
print(my_str.upper())

lst = "This will split all words into a list".split()
print(lst)

my_str = ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
print(my_str)

'This will join all words into a string'

index = 'Happy Birthday'.find('py')
print(index)

my_str = 'Happy Birthday'.replace('Birthday','Christmas')
print(my_str)

Output

tutorial reference
TUTORIAL REFERENCE
['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
This will join all words into a string
3
Happy Christmas
Navigation