Python- Strings

Welcome boys

Lets start the topic for the day.- "Strings in Python"
========================================================================
By the end of the session, you will be able to 
  • Understand the string data type
  • definition and purpose of string data type
  • Ways to access strings and their subsets
  • Apply the concatenation operator and predict the output.
=======================================================================

Please connect to me via Google meet. Click on the following link:

Python Strings

  • Strings in Python are identified as a contiguous set of characters represented in the quotation marks. 
  • Python allows for either pairs of single or double quotes. 
  • Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
  • The plus (+) sign is the string concatenation operator.
  •  The asterisk (*) is the repetition operator. For example −
(To check the output,Click on "Live Demo" )
#!/usr/bin/python

str = 'Hello World!'

print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string
This will produce the following result −
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
EXERCISE
  1. What can you say about the string data type?
  2. Is there a difference between "Geeta" and 'Geeta'? Justify.
  3. Predict the output of the following:
    1. print("SCS"*5)
    2. print(("SCS"," ")*5)
    3. print("I"+"Love"+"Columbas")
    4. print("I","Love","Columbas")
    5. print("I\n"+"Love\n"+"Columbas\n")
    6. print("I\t"+"Love\t"+"Columbas\t")
  4. Suppose
str='I LOVE COLUMBAS'
Predict the output of the following:
print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:14]    # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str[0:5] + " MUSIC"


Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW