Python3 String

Strings are the most commonly used data type in Python. We can use quotation marks ( ' or " ) to create strings.

Creating a string is easy, just assign a value to the variable. E.g:

var1 = 'Hello World!'
var2 = "TutorialFish"

Python Access Value in String

Python does not support single-character types, and single-characters are also used as a string in Python.

When Python accesses substrings, you can use square brackets [ ] to intercept strings. The syntax format of string interception is as follows:

variable [head index: tail index]

The index value starts with 0 , and -1 is the starting position from the end.


str = "TRY IT"
str[0] = 'T'
str[1] = 'R'
str[2] = 'Y'
str[3] = ' '
str[4] = 'I'
str[5] = 'T'
str[:] = 'TRY IT'
str[0:] = 'TRY IT'
str[:6] = 'TRY IT'
str[:3] = 'TRY'
str[0:2] = 'TR'
str[1:4] = 'RY '

Examples are as follows:

Examples (Python 3+)

var1 = 'Hello World!'
var2 = "Try It"
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])

Execution results of the above examples:

var1[0]:  Hvar2[1:5]:  ry I

Python String Update

You can intercept a part of the string and concatenate it with other fields, as in the following example:

Examples (Python 3+)


var1 = 'Hello World!'

print("Updated string: ", var1[:6] + 'TutorialFish!')

Execution results of the above examples

Updated string:  Hello TutorialFish!

Python Escape Characters

When you need to use special characters in characters, Python uses backslash \ to escape characters. The following table:

Escape CharacterDescriptionExample
\ (At the end of the line)Line continuation
>>> print("line1 \
... line2 \
... line3")
line1 line2 line3
>>> print("\\")\
>>> print('\'')'
\"Double quotes
>>> print("\"")"
\aRing the bell
>>> print("\a")
The computer makes a sound after execution.
>>> print("Hello \b World!")
Hello World!
>>> print("\000")
>>> print("\n")
\vVertical tab
>>> print("Hello \v World!")
Hello        World!
\tHorizontal tab
>>> print("Hello \t World!")
Hello      World!>>>
\rPress Enter. To move the content after \r to the beginning of the string, and replace the characters at the beginning one by one, until the content after \r is completely replaced.
>>> print("Hello\rWorld!")
>>> print('google tutorialfish bing\r123456')
123456 tutorialfish bing
\fPage change
>>> print("Hello \f World!")
Hello        World!
\yyyAn octal number, y represents characters from 0 to 7, for example: \012 represents line feed.
>>> print("\110\145\154\154\157\40\127\157\162\154\144\41")
Hello World!
\xyyHexadecimal number, starting with \x, character represented by y, for example: \x0a represents newline
>>> print("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21")
Hello World!
\otherOther characters are output in normal format

Python String Operators

In the following table, the value of the variable a is the string "Hello", and the value of the b variable is "Python":

+String concatenation

a + b output:


*Repeat output string

a*2 output result:


[]Get the characters in the string by indexa[1] output result e
[:]To intercept a part of the string, follow the principle of left closed and right open , str[0:2] does not contain the third character.a[1:4] output result ell
inMember operator - returns True if the string contains the given character'H' in a output is True
not inMember operator - returns True if the string does not contain the given character'M' not in a output result is True
r/ROriginal string-Original string: All strings are used directly literally, without escaping special or unprintable characters. The original string has almost the same syntax as the ordinary string except that the letter r is added before the first quotation mark of the string (upper and lower case).
print(r '\n')
print(R '\n')
%Format stringPlease see the next section.

Examples (Python 3+)


a = "Hello"
b = "Python"

print("a + b output result:", a + b)
print("a * 2 output result:", a * 2)
print("a[1] output result:", a[1])
print("a[1:4] output result:", a[1:4])

if("H" in a):
    print("H in variable a")
    print("H is not in variable a")

if("M" not in a):
    print("M is not in variable a")
    print("M is in variable a")

The output of the above example is:

a + b output result: HelloPythona * 2 output result: HelloHelloa[1] output result: ea[1:4] output result: ellH in variable aM is not in variable a\n\n

Python String Formatting

Python supports the output of formatted strings. Although very complex expressions may be used in this way, the most basic usage is to insert a value into a string with the string format character %s.

In Python, string formatting uses the same syntax as the sprintf function in C.

Example (Python 3+)

print("My name is %s %d years old this year!" % ('Jake', 10))

The output of the above example:

My name is Jake 10 years old this year!

Python string formatting symbols:

      %c Formatting characters and their ASCII codes
      %s Format string
      %d Format integer
      %u Format unsigned integer
      %o Format an unsigned octal number
      %x Format an unsigned hexadecimal number
      %X Format an unsigned hexadecimal number (uppercase)
      %f Format floating point numbers, you can specify the precision after the decimal point
      %e Format floating point numbers in scientific notation
      %E Same function as %e, use scientific notation to format floating point numbers
      %g Shorthand for %f and %e
      %G Shorthand for %f and %E
      %p Format the address of the variable with a hexadecimal number

Format operator auxiliary instructions:

*Define width or decimal point precision
-Used for left alignment
+Show plus sign (+) in front of positive numbers
<sp>Display a space before positive numbers
#Display zero ('0') in front of the octal number, and display '0x' or '0X' in front of the hexadecimal number (depending on whether you are using 'x' or 'X')
0The displayed number is filled with '0' instead of the default space
%'%%' outputs a single'%'
(var)Mapping variables (dictionary parameters)
mnm is the minimum total width of the display, n is the number of digits after the decimal point (if available)

Starting from Python2.6, a new function str.format() has been added for formatting strings, which enhances the function of string formatting.

Python Triple Quotes

Python triple quotation marks allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters. Examples are as follows:

Examples (Python 3+)

para_str = """This is an example of a multi-line string
Multi-line strings can use tabs
TAB (\t ).
You can also use the newline character [\n ].
print (para_str)

The execution result of the above example is:

This is an example of a multi-line stringMulti-line strings can use tabsTAB (    ).You can also use the newline character [ ].

The triple quotation marks free programmers from the quotation marks and special strings, keeping a small piece of string format throughout the so-called WYSIWYG (What You See Is What You Get) format.

A typical use case is when you need a piece of HTML or SQL, then use string combination, special string escape will be very cumbersome.

errHTML = '''
Friends CGI Demo</TITLE></HEAD>

CREATE TABLE users (  
login VARCHAR(8), 


f-string was added after python3.6. It is called a literal format string, which is the syntax of a new format string.

We used to use percent signs (%) before:


>>> name = 'TutorialFish'
>>> 'Hello %s' % name
'Hello TutorialFish'

The f-string format string starts with f and is followed by a string. The expression in the string is wrapped in curly braces {}, which will replace the calculated value of the variable or expression. Examples are as follows:


>>> name ='TutorialFish'
>>> f'Hello {name}' # Replace variable
'Hello TutorialFish'
>>> f'{1+2}' # Use expression

>>> w = {'name': 'TutorialFish', 'url': ''}
>>> f'{w["name"]}: {w["url"]}'

In this way, it is obviously simpler, and there is no need to judge whether to use %s or %d.

In the Python 3.8 version, the = symbol can be used to concatenate operation expressions and results:


>>> x = 1
>>> print(f'{x+1}')   # Python 3.6

>>> x = 1
>>> print(f'{x+1=}')   # Python 3.8

Unicode String

In Python2, ordinary strings are stored as 8-bit ASCII codes, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u .

In Python3, all strings are Unicode strings.

Python String Built-In Functions

Python's string commonly used built-in functions are as follows:

NumMethod and Description

converts the first character of the string to uppercase


center(width, fillchar)

Returns a string with the specified width in the center, fillchar is the filled character, and the default is a space.

count(str, beg=0, end=len(string))

Returns the number of occurrences of str in string, if beg or end is specified, returns the number of occurrences of str in the specified range

bytes.decode(encoding="utf-8", errors="strict")

There is no decode method in Python3, but we can use the decode() method of the bytes object to decode a given bytes object, which can be encoded and returned by str.encode().

encode(encoding='UTF-8', errors='strict')

Encode the string in the encoding format specified by encoding. If an error occurs, a ValueError will be reported by default, unless errors specify'ignore' or'replace'

endswith(suffix, beg=0, end=len(string))
checks whether the string ends with obj, if beg or end is specified, checks whether the specified range ends with obj, if yes, returns True, otherwise returns False.



Convert the tab symbol in the string to a space. The default number of spaces for the tab symbol is 8.

find(str, beg=0, end=len(string))

Check if str is contained in the string, if the range beg and end are specified, check if it is contained within the specified range, if it contains, return the starting index value, otherwise return -1

index(str, beg=0, end=len(string))

Same as the find() method, except that an exception will be reported if str is not in the string.


If the string has at least one character and all characters are letters or numbers, it returns True, otherwise it returns False


If the string has at least one character and all characters are letters or international characters, it returns True, otherwise it returns False


If the string contains only numbers, it returns True, otherwise it returns False.


If the string contains at least one case-sensitive character, and all these (case-sensitive) characters are lowercase, return True, otherwise return False


If the string contains only numeric characters, it returns True, otherwise it returns False


If the string contains only blanks, it returns True, otherwise it returns False.


If the string is titled (see title()), it returns True, otherwise it returns False


If the string contains at least one case-sensitive character, and all these (case-sensitive) characters are uppercase, return True, otherwise return False


Use the specified string as the delimiter to merge all the elements (the string representation) in seq into a new string


Returns the length of the string

ljust(width[, fillchar])

Return a new string with the original string left-aligned and filled to the length width with fillchar. The fillchar defaults to spaces.


Convert all uppercase characters in the string to lowercase.


Trim the spaces or specified characters on the left side of the string.


Create a conversion table for character mapping. For the simplest call method that accepts two parameters, the first parameter is a string, which represents the character to be converted, and the second parameter is also a string that represents the target of the conversion.


Returns the largest letter in the string str.


Returns the smallest letter in the string str.

replace(old, new [, max])

Replace old in the string with new. If max is specified, the replacement will not exceed max times.

rfind(str, beg=0, end=len(string))

It is similar to the find() function, but searches from the right.

rindex( str, beg=0, end=len(string))

Similar to index(), but starts from the right.

rjust(width, [, fillchar])

Return a new string with the original string right-aligned and filled to the length width with fillchar (default empty space)


Delete spaces or specified characters at the end of the string.

split(str="", num=string.count(str))

Use str as the separator to intercept the string, if num has a specified value, only num+1 substrings will be intercepted


Separate by lines ('\r','\r\n', \n'), and return a list containing each line as an element. If the parameter keepends is False, the newline character is not included, and if it is True, the newline character is retained.

startswith(substr, beg=0, end=len(string))

Check whether the string starts with the specified substring substr, if yes, return True, otherwise return False. If beg and end specify values, check within the specified range.


Perform lstrip() and rstrip() on the string


Convert uppercase to lowercase in a string, and lowercase to uppercase


Return a "titled" string, which means that all words start with uppercase and the rest of the letters are lowercase (see istitle())

translate(table, deletechars="")

Convert the characters of string according to the table given by str (containing 256 characters), and put the characters to be filtered out in the deletechars parameter


Convert lowercase letters in a string to uppercase

zfill (width)

Returns a string of length width, the original string is right-aligned, and the front is filled with 0


Check whether the string contains only decimal characters, if it is true, otherwise it returns false.