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 −
#!/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
str='I LOVE COLUMBAS'
- What can you say about the string data type?
- Is there a difference between "Geeta" and 'Geeta'? Justify.
- Predict the output of the following:
- print("SCS"*5)
- print(("SCS"," ")*5)
- print("I"+"Love"+"Columbas")
- print("I","Love","Columbas")
- print("I\n"+"Love\n"+"Columbas\n")
- print("I\t"+"Love\t"+"Columbas\t")
- Suppose
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"
Pls join the meeting immediately
ReplyDeletepls join the google meet class immediately else attendance cant be given. explanation in the meeting
ReplyDeleteGood morning ma'am
ReplyDeleteGood morning
ReplyDeleteGood morning ma'am
ReplyDelete-Geet
good ,morning mam
ReplyDelete