A string is a sequence of characters. In other words, a string is basically a bunch of words and/or symbols.
You will use strings in almost every program that you will write, hence it is very important to understand strings properly.
We will now learn the various ways of using strings in Python:
You can specify strings using single quotes such as
'Quote me on this'
.
Strings in double quotes work exactly the same way as
strings in single quotes. An example is
"What's your name?"
You can specify multi-line strings using triple quotes. You can use single quotes and double quotes freely within the triple quotes (that is quite a mouthful to say!). An example is shown below.
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
Suppose you wanted to use a string which contains a
single quote ('), how will you specify the string? For
example, the string is What's your
name?
You cannot use 'What's your
name?'
since Python will be confused as to where
the string starts and ends. In this case, we need to tell
Python that the second single quote does not specify the end of
the string. We do this by putting a backslash before that
single quote. So, we use the string as
'What\'s your name?'
. The sequence
\'
is called an escape sequence because
it causes the single quote to escape its
special meaning. There are many other types of escape sequences
as well.
Another way of specifying that string would be using
double quotes - "What's your
name?"
. You can use an escape sequence
\"
to include a double quote inside a
double-quoted string. To include a backslash itself, use the
escape sequence \\
.
We have seen that triple-quoted strings can be used to
specify multi-line strings but what if we wanted to create
multi-line strings without using triple quotes? We can achieve
that using the newline escape sequence \n
which literally represents the start of a new
line. Similarly, a tab can be represented using
\t
. There are many more escape sequences but
I am mentioning only the important ones here.
One additional note - a single backslash at the end of the line indicates that the string is continued in the next line but no newline is added. For example:
"This is the first sentence. \
This is the second sentence."
is equivalent to "This is the first
sentence. This is the second sentence."
If you need to specify some strings as-is, that is,
without any special processing for escape sequences, then you
can specify the string as a raw string by
prefixing r
or R
to the
string. An example is r"Newlines are indicated by
\n"
.
Unicode is a standard way of writing text in multiple
human languages. If you want to write text in your native
language such as Kannada or Italian, then you need to have a
Unicode-enabled text editor. Similarly, Python gives you the
ability to handle Unicode text, all you need to do is prefix
u
or U
to the string. An
example is u"This is a Unicode
string"
.
Remember to use Unicode strings when you are dealing with text files, especially when you know that the file will contain text written in languages other than English.
Immutable means 'cannot be changed'. This means that once you create a string, you cannot change it. However, you can create new strings based on this string.
If you place two string literals side by side, they are
automatically concatenated i.e. a new string equivalent to
joining of the two strings is given by Python. For example,
'What\'s' 'your
name?'
is concatenated to get
"What's your name?"
There is no separate char
datatype
in Python. There is no real need for it and I'm sure you
won't miss it.
Remember that single-quoted strings and double-quoted strings are the same, they do not differ in any way.
Always use raw strings when dealing with regular
expressions, otherwise, a lot of backwhacking may be required.
For example, backreferences can be referred as
'\\1'
or as
r'\1'.