Sunday, December 1, 2019

Using Python Shell for Working with Strings: Part -1


In previous article we have learnt about how to use Python Shell as a simple calculator using numbers. In this article we are going to learn how we can use Python Shell to work with Strings, as it helps us to express Strings in many ways.

Points to ponder:

1. Strings can be enclosed in single quote ('.......') or double quote (".......").
2. Back Slash \ can be used to avoid or escape the quote symbols.

Example 1:
>>> 'Hello World' # using single quotes
'Hello World'
>>> "Hello World!" # using double quotes
'Hello World!'
>>> 'Meetu's'
SyntaxError: invalid syntax
>>> 'Meetu\'s'  # use \' to escape the single quote...
"Meetu's"
>>> "Meetu's" # ...or use double quotes instead
"Meetu's"
>>> '"Yes", She Said'
'"Yes", She Said'
>>> 

Sometimes working directly with Single Quotes, Double Quotes and using Back Slash for more formatted output seems tiresome and clumsy. Which leads to many formatting errors and harder to recognize them, also decreasing its readability. Here comes the Print Function to our rescue. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters see example 2


Example 2:
>>> '"Meetu\'s," Blog' # use \' to escape the single quote...
'"Meetu\'s," Blog'
>>> print ('"Meetu\'s," Blog') # use \' to escape the single quote...
"Meetu's," Blog
>>> myString = 'First line.\nSecond line.' # \n means newline
>>> myString  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(myString) # with print(), \n produces a new line
First line.
Second line.
>>>


Sometimes we would prefer that special characters starting with \ should not be interpreted as special characters rather they must be treated as simple strings. This task can be achieved by a very simple method you just need to place a character r before placing the first quote in string. This r stands for raw strings  

Example 3:
>>> print('C:\Python\name') # here \n means newline!
C:\Python
ame
>>> print(r'C:\Python\name') # note the r before the quote
C:\Python\name
>>>

Strings concatenation (adding two or more strings)  can be done by using + operator, also we can repeat a string for a specified no of times using * operator take a loko on following Example no 4

Example 4:

>>> 'Meetu' + ' ' + 'Choudhary'  # Concatenation using + operator  
'Meetu Choudhary'
>>> print('Meetu' + ' ' + 'Choudhary') # Concatenation using + operator in print () function 
Meetu Choudhary

>>> 2 * 'Meetu Choudhary' # Multiplication using * operator 
'Meetu ChoudharyMeetu Choudhary'
>>> print (2 * 'Meetu Choudhary') # Multiplication using * operator  using print() function

Meetu ChoudharyMeetu Choudhary
>>> m='Meetu' # a String Variable

>>> m + ' Choudhary' #Concatenation using Variable

'Meetu Choudhary'

>>> print (m + ' Choudhary') #Concatenation using Print and Variable

Meetu Choudhary
>>> print (2 * m)  #Multiplication using print() function and variable

MeetuMeetu
>>> 2*m  #Multiplication using variable

'MeetuMeetu'
>>>


Working with strings will be continued in Part II visit here

No comments:

Post a Comment