Python provides an
str data type to store text string content.
The string is nothing but an array of bytes where each byte represents alphabetic or Unicode characters.
The string is a group of characters enclosed in double or single quotes. There is no Characters data type in python. Each character is represented in bytes. String are group of characters enclosed in double quotes or single quotes
str= "test" str1= 'test'
The above two strings both are same, though string value is enclosed with double and single quotes.
Python String datatype
A string variable can be declared using
str datatype or string literal.
let’s declare a string variable using single, double, and triple quotes.
# Double quotes name="john" # Single quotes name1='john' # Triple quotes name2='''john''' print(type(name)) print(type(name1)) print(type(name2))
<class 'str'> <class 'str'> <class 'str'>
How do you create a multiline string in Python?
Sometimes, you want to store long lines of string spanned in multiple lines in a variable.
The string is split into multiple lines.
There are different ways we can declare multi-line strings in python.
The first way, use a triple single enclosed wrapped for a string.
Here is an example to use triple single quotes multiline string
message = '''This is long line of text line1 This is long line of text line2 This is long line of text line3 ''' print(message)
The second way use triple double quotes
message1 = """This is long line of text line1 This is long line of text line2 This is long line of text line3 """ print(message1)
The third way use a multi-line string wrapped inside brackets
multi-line String is wrapped inside a
() and each line is enclosed with double quotes as given below
message = ("This is long line of text line1" "This is long line of text line2" "This is long line of text line3") print(message)
This is long line of text line1This is long line of text line2This is long line of text line3
The output does not contain newline characters, you can add new line(\n)characters
The fourth way, use the backslash character Each string line is wrapped with a backslash character.
message = "This is long line of text line1\ This is long line of text line2 \ This is long line of text line3" print(message)