Skip to content

Latest commit

 

History

History
151 lines (113 loc) · 2.64 KB

File metadata and controls

151 lines (113 loc) · 2.64 KB

Lecture-6 Comments And String Methods in Python

Comments in python

Comments in Python are the lines in the code that are ignored by the compiler during the execution of the program.

  1. Single Line Comment (#)

A single-line comment begins with a hash (#) symbol. The single-line comment is used to comment only one line of the code.

#This is sample program 
print("roadtocode4u")
  1. Multiline Comment (""" """)

In Python Triple double quote (""") and single quote (''') are used for Multi-line commenting.Multi-line comment is useful when we need to comment on many lines.

"""
This is a comment
written in
more than just one line
"""
print("roadtocode4u")

String Methods

  1. Upper()

Converts a string into upper case.

myString="roadtocode4u"
newString=myString .upper()
print(newString)
  1. Lower()

Converts a string into lower case.

myString="ROADTOCODE4U"
newString=myString .lower()
print(newString)
  1. Strip()

Removes white space from the end of String

str1="ROADTOCODE4U"
str2="ROADTOCODE4U             "
print(str1)
print(str2.strip())
  1. Replace()

Replace in string

myString="roadtocode4u"
newString=myString.replace("o","#")
print(newString)
  1. split()

Splits the string at the specified separator, and returns a list.

student="Harshada,Aniket,Prachi,Anjali"
print(student.split(','))

Array

Collection of similar data type

myString="India"
print(myString[2])

Escape character

An escape character is a backslash \ followed by the character you want to insert.

sentence="I\'m Good .She said , How Are You ?"
print(sentence)

🏠 HomeWork

1️⃣Write a program to implement upper(), lower(), strip() and replace() function on given string.

👁 Show Answer

# Upper
name="Vedika"
newString=name.upper()
print(newString)

# Lower
name="VEDIKA"
newString=name.lower()
print(newString)

#Strip 
name1="Vedika"
name2="Vaibhavi "
print(name1)
print(name2.strip()) 

#repalce
name="road to code"
newString=name.replace("o","#")
print(newString)

2️⃣Write a program to implement escape character to use single quote and double quote in the same string.

👁 Show Answer

sentence="I\'m Good .She said , How Are You ?"
print(sentence)   #Escape Character

🔗 Some Useful Links

📖 References