Python Strings

A string represents a group of characters. Strings are important because most of the data that we use in daily life will be in the form of strings. For example, the names of persons, their addresses, vehicle numbers, their credit card numbers, etc. are all strings. In Python, the str datatype represents a string. Since every string comprises several characters, Python handles strings and characters almost in the same manner. There is no separate datatype to represent individual characters in Python.

Create String

We can create a string in Python by assigning a group of characters to a variable.

CopiedCopy Code
s1 = 'Welcome to Python Programming ' 
s2 = "Welcome to Python Programming"
s3 = '''Welcome to Python Programming'''
s4 = """Welcome to Python Programming"""
						

Thus, triple single quotes or triple double quotes are useful to create strings which span into several lines.

String with quotation

It is possible to display quotation marks to mark a sub string in a string. In that case, we should use one type of quotes for outer string and another type of quotes for inner string as:

s1 = 'Welcome to "Python" Programming'

print(s1)

The preceding lines of code will display the following output:

CopiedCopy Code
Welcome to "Python" Programming

It is possible to use escape characters like \t or \n inside the strings. The escape character \t releases tab space and the escape character \n throws cursor into a new line.

String with tab and new-line chars

CopiedCopy Code
s1 = "Welcome to\tPython\nProgramming"
print(s1)
Welcome to      Python
Programming