UNIT-3
Strings revisited
We know that a String is a sequence of characters. Strings in Python are immutable. You can’t
change a string in place, but you can copy parts of strings to another string to get the same effect.
Python string is created by enclosing characters in either single quotes or double quotes, as
demonstrated in the following:
>>> ‘Python is fun’
‘Python is fun’
>>> “Python is easy”
‘Python is easy’
The interactive interpreter echoes strings with a single quote, but all are treated exactly the same
by Python.
You must be wondering why have two kinds of quote characters?
Try this program ‘Strings_demo.py‘ and see the ouput, you might figure out the reason behind
using two kinds of quotes.
,In case you couldn’t notice the reason is so that you can create strings containing quote
characters. You can have single quotes inside double-quoted strings, or double quotes inside
single-quoted strings. The output for Strings_demo.py would further clarify –
Even three single quotes (”’) or three double quotes (“””) can also be used. Their most common
use is to create multi-line strings. Try this on your editor and see the output-
note = ”’You should know that,
… Python is a snake;
… But this programming language,
… has nothing to do with the snake as”
… it is named after Monty Python.”’
print (note)
,If you wish you can create an empty string as well. The following program demonstrates it’s
usage-
See the output: Total users: 150
So want to build a string from other strings, and you need to start with a blank string. Also you
may have noticed that you can convert other Python data types to strings by using the str()
function, as we did in our previous program (message += str(users)).
Escape sequences
By preceding a character with a backslash (\), you give it a special meaning.
, 1. The most common escape sequence is \n, which means to begin a new line. With this you can
create multi-line strings from a one-line string.
Example: print(“Languages: \n Python \n C \n Java”)
2. escape sequence \t (tab) used to align text
Example:
Strings revisited
We know that a String is a sequence of characters. Strings in Python are immutable. You can’t
change a string in place, but you can copy parts of strings to another string to get the same effect.
Python string is created by enclosing characters in either single quotes or double quotes, as
demonstrated in the following:
>>> ‘Python is fun’
‘Python is fun’
>>> “Python is easy”
‘Python is easy’
The interactive interpreter echoes strings with a single quote, but all are treated exactly the same
by Python.
You must be wondering why have two kinds of quote characters?
Try this program ‘Strings_demo.py‘ and see the ouput, you might figure out the reason behind
using two kinds of quotes.
,In case you couldn’t notice the reason is so that you can create strings containing quote
characters. You can have single quotes inside double-quoted strings, or double quotes inside
single-quoted strings. The output for Strings_demo.py would further clarify –
Even three single quotes (”’) or three double quotes (“””) can also be used. Their most common
use is to create multi-line strings. Try this on your editor and see the output-
note = ”’You should know that,
… Python is a snake;
… But this programming language,
… has nothing to do with the snake as”
… it is named after Monty Python.”’
print (note)
,If you wish you can create an empty string as well. The following program demonstrates it’s
usage-
See the output: Total users: 150
So want to build a string from other strings, and you need to start with a blank string. Also you
may have noticed that you can convert other Python data types to strings by using the str()
function, as we did in our previous program (message += str(users)).
Escape sequences
By preceding a character with a backslash (\), you give it a special meaning.
, 1. The most common escape sequence is \n, which means to begin a new line. With this you can
create multi-line strings from a one-line string.
Example: print(“Languages: \n Python \n C \n Java”)
2. escape sequence \t (tab) used to align text
Example: