1. a = "Hello, World!
"
print(a[1]) // prints first character in string i.e., ‘e’
2. for x in "banana":
print(x) // prints in column order
3. a = "Hello, World!"
print(len(a)) //prints number of characters
4. txt = "New delhi is capital of India"
print("India" in txt) // prints true if string is present in given text
(or)
txt = "New delhi is capital of India"
if "India" in txt:
print("Yes, 'India' is present.")
5. txt = " New delhi is capital of India!"
print("india" not in txt) // checks whether string is present or not
Slicing strings
6. b = "Hello, World!"
print(b[2:5]) // prints characters from position2 to position 5 (not included)
print(b[:5]) //prints starting to 5 characters
print(b[2:]) //prints string from position 2
print(b[-5:-2]) //prints in reverse order
print(a.upper()) //prints in upper case letters
print(a.lower()) // prints in lower case letters
print(a.strip()) //Removes white space from the beginning or end
print(a.replace("H", "J")) //replaces H with J
print(a.split(",")) //splits every string
String Concatenation
7. a = "Hello"
b = "World"
c=a+b
print(c)
8. a = "Hello"
b = "World"
c=a+""+b
print(c)
9. quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
Note: txt = "Hyderabad is a “beautiful” city!” //gives error
txt = "Hyderabad is a \“beautiful\” city!”
1. Finding number of characters using functions:
def string_length(str1):
count = 0
for char in str1:
count += 1
return count
print(string_length('jntuhuceh'))
2. Reversing characters in string:
string = "enter some text"
s = string.split()[::-1] # reversing words in a given string
l=[]
for i in s:
l.append(i) # apending reversed words to l
print(" ".join(l)) # printing reverse words
3. Removing character from string:
str = "python programming"
print ("The original string is : ", str)
new_str = " "
for i in range(len(str)):
if i != 6: # Removing char at pos 6
new_str = new_str + str[i]
print ("The string after removal of i'th character : ", new_str)
4. Printing only even length words in string
n="This is a python programming lab"
s=n.split(" ") #splitting the words in a given string
for i in s:
if len(i)%2==0: #checking the length of words
print(i)
5. Counting number of lines, words, characters from a text file:
def countWords(fileName):
numwords = 0
numchars = 0
numlines = 0
with open(fileName, 'r') as file:
for line in file:
wordlist = line.split()
numlines += 1 # Assignment operator:numlines=numlines+1
numwords += len(wordlist)
numchars += len(line)
print ("Words: ", numwords)
print ("Lines: ", numlines)
print ("Characters: ", numchars)
if _name_ == '_main_':
countWords('filename’)
6. Program to read text file and display all words starting with vowel
f = open("filename","r")
data = f.read()
for word in data.split():
if word[0] in ("a","e","i","o","u",”A”, “E”, “I”, “O”, “U”):
print(word)
7. Program to display all words starting with vowel in a text
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"] # initializing list
print("The original list is : " , str(test_list)) # printing original list
res = [ ] # Intialise Empty list
vow = "aeiou"
for sub in test_list:
flag = False
for ele in vow: # checking for begin character whether vowel or not
if sub.startswith(ele):
flag = True
break
if flag:
res.append(sub)
print("The extracted words : " , str(res)) # printing result
Exercise:
1. Python program for removing i-th character from a string
2. program to count number of vowels using sets in given string
3. Check if a Substring is Present in a Given String
4. Remove substring from String