Sunday, 19 March 2017

Python programming part-5 Strings

Strings : part-1

If you want to use text in Python , you have to use a string .
a string is created by entering text between two single or double quotation mark.

When the python console displays a string, it generally uses single quotes . The delimiter used for a string doesnot affect how behaves in any way .

>>> "Python is fun!"
'Python is fun!'
>>> 'Always look on the bright side of life'
'Always look on the bright side of life'


Question:
Complete the code to create a string  containing "Hello World" .
>>>"Hello _____"
'Hello World'

____________________________________________________

Strings : part-2

Some characters can't be directly included in a string . For instance, double quotes can't be directly included in a  double quote string ; this would cause it to end prematurely.

Characters like these must be escaped by placing a backslash before them.
Other common characters that must be escaped are newlines and backslashes.
Double quotes only need to be escaped in double quote string , and the same is true for single quote strings.

>>>'Brain\'s mother : He\'s not the Messiah.
He\'s a very naughty boy!'
'Brains's mother : He's not the Messiah . He's a very naughty boy!'

\n represents a new line.

_______________________________________

Newlines 

Python provided an easy way to avoid manually writing "\n" to escape newlines in a string . Create a string with three sets of quotes , and newlines that are created by pressing Enter are automatically escaped for you .

>>>"""Customer: Good morning.
Owner: Good morning , Sir. Welcome to the National Cheese Emporium."""

'Customer: Good morning .\n Owner : Good morning , Sir . Welcome to the National Cheese Emporium.'


Question:
Fill the missing part of the output.

>>>"""First line.
second line"""
'First line ___second line'

____________________________________________________

                                                                                                          


EmoticonEmoticon