Textual data in Python in represented by string objects. Strings are immutable sequences of unicode code points. Strings in Python can be written in three ways:
1. Single quotes
- Single quoted strings allows embedded double quotes.
- Single quoted strings do not span multiple lines.
txt = 'hello world from Python language' txt = 'hello world from "Python" language'
2. Double quotes
- Double quoted strings allow embedded single quotes.
- Double quoted strings do not span multiple lines.
txt = "hello world from Python language" txt = "hello world from 'Python' language"
3. Triple quotes
- Triple quoted strings allow embedded single and double quotes.
- Triple quoted strings may span multiple lines and all whitespace between the triple quotes will be included in the string literal.
- Triple quotes strings are used to write doc strings.
txt=''' hello world from Python''' txt=""" hello world from Python"""
x = 'hello world' print(type(x)) y = "hello world" print(type(y)) z = '''hello world from Python''' print(type(z))
Output
<class 'str'>
<class 'str'>
<class 'str'>
Note
Character
data type like the language Java is not there in Python. So if you write'C'
, then it is still a string of length 1.- String type is immutable. All string methods returns new values. They do not change the original string.
Accessing characters in a string
As we know that string is a sequence of characters, we can access characters of the string using indexing. Index starts from 0. Trying to access a character out of index range will raise an IndexError
. The index must be an integer. We can’t use floats or other types, this will result into TypeError
.
x = 'hello' print(x[0]) print(x[1]) print(x[2]) print(x[3]) print(x[4])
Output
h
e
l
l
o
Negative Indexing
Python support negative indexing. Index of -1
represents last character, -2
represents second last character and so on.
x = 'hello' print(x[-1]) print(x[-2]) print(x[-3]) print(x[-4]) print(x[-5])
Output
o
l
l
e
h
Escape Character
There is a certain format to write a string. You can not use certain characters within quotes while writing a string. For example, you can not use a double quote in a double quoted string. For example:
#Double quotes inside double quoted string is not allowed txt = "This is a "hello world" greeting message from Learnitweb"
To fix this problem we can escape double quotes. An escape character is a backslash \
followed by the character you want to insert.
The above string can be written as:
txt = "This is a \"hello world\" greeting message from Learnitweb"
Other recognized escape sequences in Python are:
Escape Sequence | Meaning |
---|---|
\newline | Backslash and newline ignored |
\\ | Backslash () |
\’ | Single quote (‘) |
\” | Double quote (“) |
\a | ASCII Bell (BEL) |
\b | ASCII Backspace (BS) |
\f | ASCII Formfeed (FF) |
\n | ASCII Linefeed (LF) |
\r | ASCII Carriage Return (CR) |
\t | ASCII Horizontal Tab (TAB) |
\v | ASCII Vertical Tab (VT) |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |