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 Notation | Description |
---|---|
\a | Bell or alert |
\b | Backspace |
\cx | Control-x |
\C-x | Control-x |
\e | Escape |
\f | Formfeed |
\M-\C-x | Meta-Control-x |
\n | Newline |
\nnn | Octal notation, where n is in the range 0.7 |
\r | Carriage return |
\s | Space |
\t | Tab |
\v | Vertical tab |
\x | Character x |
\xnn | Hexadecimal 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.