Python string datatype

A sequence of characters is known as a string. Python allows you to use single quotes ('), double quotes (") or triple quotes (""") to denote a string.

Python string datatype

A sequence of characters is known as a string. Python allows you to use single quotes (‘), double quotes (“) or triple quotes (“””) to denote a string. Only triple quotes can be used to create a string which spans multiple lines.

Example

a='This is a string'
print(a)
b="This is also a string"
print(b)
c="""This is a string
which spans
multiple lines"""
print(c)

Output

This is a string
This is also a string
This is a string
which spans
multiple lines

String operations

Python allows you to perform several string operations such as slicing, concatenating, formatting etc.

1) Example – slice

a = "Hello World!"
b = a[3:6]
print(b)

2) Example – Get character at a position

a = "Hello World!"
print(a[1])

Share Tweet Send
0 Comments
Loading...