Page 1 of 15
Home Whiteboard Online Compilers Practice Articles Jobs Tools
SQL HTML CSS Javascript Python Java C C++ PHP Scala C#
Chapters Categories
Python - Strings
MilesWeb
SEE MORE
Work Smarter, Together
In Python, a string is an immutable sequence of Unicode characters. Each character has
a unique numeric value as per the UNICODE standard. But, the sequence as a whole,
doesn't have any numeric value even if all the characters are digits. To differentiate the
string from numbers and other identifiers, the sequence of characters is included within
single, double or triple quotes in its literal representation. Hence, 1234 is a number
(integer) but '1234' is a string.
Creating Python Strings
As long as the same sequence of characters is enclosed, single or double or triple
quotes don't matter. Hence, following string representations are equivalent.
Example
>>> 'Welcome To TutorialsPoint'
'Welcome To TutorialsPoint'
>>> "Welcome To TutorialsPoint"
'Welcome To TutorialsPoint'
>>> '''Welcome To TutorialsPoint'''
'Welcome To TutorialsPoint'
>>> """Welcome To TutorialsPoint"""
'Welcome To TutorialsPoint'
Looking at the above statements, it is clear that, internally Python stores strings as
included in single quotes.
In older versions strings are stored internally as 8-bit ASCII, hence it is
required to attach 'u' to make it Unicode. Since Python 3, all strings are
https://www.tutorialspoint.com/python/python_strings.htm 1/15
Page 2 of 15
represented in Unicode. Therefore, It is no longer necessary now to add 'u'
after the string.
Advertisement
Accessing Values in Strings
Python does not support a character type; these are treated as strings of length one,
thus also considered a substring.
To access substrings, use the square brackets for slicing along with the index or indices
to obtain your substring. For example −
Open Compiler
var1 = 'Hello World!'
var2 = "Python Programming"
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])
When the above code is executed, it produces the following result −
var1[0]: H
var2[1:5]: ytho
https://www.tutorialspoint.com/python/python_strings.htm 2/15
Page 3 of 15
Updating Strings
You can "update" an existing string by (re)assigning a variable to another string. The
new value can be related to its previous value or to a completely different string
altogether. For example −
Open Compiler
var1 = 'Hello World!'
print ("Updated String :- ", var1[:6] + 'Python')
When the above code is executed, it produces the following result −
Updated String :- Hello Python
Visit our Python - Modify Strings tutorial to know more about updating/modifying strings.
Escape Characters
Following table is a list of escape or non-printable characters that can be represented
with backslash notation.
An escape character gets interpreted; in a single quoted as well as double quoted
strings.
Backslash Hexadecimal
Description
notation character
\a 0x07 Bell or alert
\b 0x08 Backspace
\cx Control-x
\C-x Control-x
\e 0x1b Escape
\f 0x0c Formfeed
\M-\C-x Meta-Control-x
\n 0x0a Newline
https://www.tutorialspoint.com/python/python_strings.htm 3/15