Search Here

Python Strings - Escape codes, string concatenation, accessing strings

Python Strings

In this tutorial, you’ll learn to properly handle strings in python, print, slice, and format them. Python strings are a sequence of characters that occur one after the other forming a meaningful word or sentence.

Post Update Timeline

Updated on : 31-07-2020

Added new topics such as Escape Codes with some more examples.

You can create a string using single, double or triple quotes

str_one = "Say Hello to Python Programming"
print(str_one)

str_two = 'Say Hello to Python Programming'
print(str_two)

str_three = '''
    Say Hello 
    to 

    Python Programming'''

print(str_three)

str_four = """
    Say Hello 
    to 
    
    Python Programming"""

print(str_four)


#PYTHON OUTPUT
Say Hello to Python Programming
Say Hello to Python Programming

  Say Hello 
  to 

  Python Programming

  Say Hello 
  to 
  
  Python Programming

Python Strings - Creating string using single, double or triple quotes

Python Strings – Creating string using single, double or triple quotes

Note

The triple quotes string are used for multiline text purpose.

Escape Codes

The Escape Codes are command and frequently used part of programming they are represented as with backslash followed by a character. Each of the escape characters has a specific purpose such as the \n is a newline character which prints upcoming character in a new line. Some of the Escape Codes are mentioned below:

Symbol Name Description Example
\n New Line Feed Prints upcoming string in a new line
>>print("Hello\nWorld")
Hello
World                    
\r Carriage Return This prints the next sequence of characters to the start off the line.
>>print("Hello\rWorld")
World                    
\t Horizontal Tab Horizontal Tab adds empty spaces from where it is mentioned.
>>print("Hello\tWorld")
Hello   World         
\v Vertical Tab Vertical Tab adds empty spaces from next to were it was mentioned.
>>print("Hello\vWorld")
Hello
        World
\b Backspace Truncates a single character and works the same way as a keyboard backspace.
>>print("Hello\bWorld")
HellWorld    
\f Form Feed also called as Page Feed Breaks the current page and continues to the next page and starts from the same position where is left.
>>print("Hello\fW\forld")
Hello
        W
        orld
\a Alert Produces beeps sound.
                    
>>print("Hello\aWorld")
HelloWorld     
\\ Backslash Escapes special characters like single and double-quotes.
>>print('Hello it\'s me')
Hello it's me 
Python string may start from single, double or 3 (”’) single, (“””) double qoutes
a = 'This is a python string...from single qoutes'
b = "This is a python string...from double qoutes"
c = '''This is a python string...from 3 single qoutes on start and finish'''
d = """This is a python string...from 3 double quotes on start and finish"""

print(a)
print(b)
print(c)
print(d)

#Python output
This is a python string...from single quotes
This is a python string...from double quotes
This is a python string...from 3 single quotes on start and finish
This is a python string...from 3 double quotes on start and finish

Accessing String

Strings can also be accessed by index just like arrays.

Example

website_name = "TheCodeLearners"
print(website_name)
print(website_name[0])
print(website_name[1])
print(website_name[2])
print(website_name[3])
print(website_name[4])
print(website_name[5])
print(website_name[6])
print(website_name[7])
print(website_name[8])
print(website_name[9])
print(website_name[10])
print(website_name[11])
print(website_name[12])
print(website_name[13])
print(website_name[14])

#Python OUTPUT
TheCodeLearners
T
h
e
C
o
d
e
L
e
a
r
n
e
r
s
String TheCodeLearners as 14 characters i.e if we count from 0 their will be 14 characters.
print(website_name[15])

#PYTHON ERROR OUTPUT
string index out of range

Escape Special Characters in String

Backslash \ is used to escape any special character in the string.

Example

string = "This string with double quotes"
e_string = "This string with \"double quotes escaped with a backslash"
a_string = 'I don\'t like this'

print(string)
print(e_string)
print(a_string)

#PYTHON OUTPUT
This string with double quotes
This string with "double quotes escaped with a backslash
I don't like this

Print string in new line use \n

Example

x = 'This will be printed in first line.\nThis will be printed in second line'
print(x)

#PYTHON OUTPUT
This will be printed in first line.
This will be printed in second line

Raw Strings

If for some cases you wanted to omit special character \ backslash then you can use raw strings by placing `r` at the start of the string

Example

string = 'Some File Path \temp\name\read'
e_string = r'Some File Path \temp\name\read'
print(string)
print(e_string)

#Python OUTPUT
Some File Path 	emp
ead
Some File Path \temp\name\read 
To Display Multiline message use ”’ or “”” and use r to omit \ backslash.

String Concatenation and Formatting

You can also concatenate strings with variables inside it.

age = 15
print("Jim is "+str(age)+" years old.")

#PYTHON OUTPUT
Jim is 15 years old.

age = 15
print("Jim is %d years old." % age)

#PYTHON OUTPUT
Jim is 15 years old.

age = 15
print("Jim is {} years old.".format(age))

#PYTHON OUTPUT
Jim is 15 years old.

age = 15
print("Jim is {new_age} years old.".format(new_age=age))

#PYTHON OUTPUT
Jim is 15 years old.

age = 15
print(f"Jim is {age} years old.")

#PYTHON OUTPUT
Jim is 15 years old.
Concatenation and Formatting is explained in detail in this post.
Summary
Review Date
Reviewed Item
Python Strings
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Language