Python String

Let’s talk about this data type str which stands for strings.

A string is simply a piece of text for example a string can be written with quotation marks i.e ‘Hello World’.

We can also make strings with double quotes, this is also a valid string i.e. “Hello World”.

Python does not support character data type, one character is also treated as a string of length one.

x = "Hello World"
y = 'Python Coding'

print(x)
print(y)

Output:

Hello World
Python Coding

Multiple Line String

We can’t write a paragraph of multiple lines using single or double quotes, so there is a triple quotation method to write a big paragraph of multiple lines.

x = '''This is a big paragraph
by using triple qoutation
in Python.
'''

print(x)

Output:

This is big paragraph
by using triple qoutation
in Python.

**We also use escape characters in between big paragraphs.


Escape Characters

Backslash NotationDescription
\aBell or alert
\bBackspace
\cxControl-x
\C-xControl-x
\eEscape
\fFormfeed
\M-\C-xMeta-Control-x
\nNewline
\nnnOctal notation, where n is in the range 0.7
\rCarriage return
\sSpace
\tTab
\vVertical tab
\xCharacter x
\xnnHexadecimal notation, where n is in the range 0.9, a.f, or A.F

Accessing Values in Strings

We can access characters or substring from a string using index number. This is also called String Slicing. For example:

x = 'Python'
    #012345

print(x[2])
print(x[0:4]) #start index : end index+1

Output:

t
Pyth

String Concatenation

Adding two or more string is called String Concatenation. This only works with Strings, we can’t a string and a number.

Scroll to Top