String
String
Q6. Q9.WAP to check the given string is palindrome or not. E.g. NITIN
Palindrome[rev] is: NITIN
What would following expression return? s="NITIN"
(b) ”HelloWorld”. Upper().lower() (b) ”Hello World”.lower().upper() s1=s[::-1]
( c) ”HelloWorld”. Find(“Wor”,1,6) (d) ”Hello World”. Find(“Wor”) if s==s1:
print("String is pallindrome")
(e) ”Hello World”. Find(“wor”) else:
(f) ”Hello World”. isalpha() print("String is not pallindrome")
(g) ”Hello World”.isalnum()
(h) ”Hello World”.isdigit() (i) “123FGH”.isdigit() Q10 WAP to print the number of occurrences of a substring into a line.
s="johny johny yes papa, johny johny yes papa"
z=input("Enter a substring")
Ans: (a) 'hello world' (b) 'HELLO WORLD'
s1=s.split()
(c) -1 (d) 6 print(s1)
(e) -1 (f) False for i in range(len(s1)):
if s1[i]==z:
(g) False (h) False
c+=1
I) False print("Total no.of SubString is",c)
the uppercased string from the “True” if all the characters in the
given string. It converts all string are uppercase; otherwise, It
lowercase characters to the returns “False.”
Q11.Write a program that reads a string and prints a string that capitalizes every uppercase.
other letter in the string example student becomes StUdEnT EG: EG:
s="student" a = 'we are in the endgame a = 'WE ARE IN THE ENDGAME
for i in range(len(s)): now' NOW#'
if i%2==0: print(a. upper()) print(a.isupper())
z=s[i].capitalize()
print(z,end=" ")
else: Output Output
print(s[i],end=" ") WE ARE IN THE ENDGAME True
Q12.Write a program that reads a string and count : NOW
b. Number of space in given string b. Number of Vowels in given string
c. Number of words in given string d. Number of characters in given string title () capitalize ()
s="johny johny yes papa johny johny yes papa" title() function in Python is In Python, the capitalize() method
s1=s.split() the Python String Method converts the first character of a string
print("String is: ",s) which is used to convert the to a capital (uppercase) letter. If the
for i in range(len(s)): first character in each word to string has its first character as capital,
if s[i]==" ": Uppercase and remaining then it returns the original string.
space+=1 characters to Lowercase
if s[i] in'aeiouAEIOU': in the string and returns a
vowels+=1 new string.
EG: EG:
print("Total no. of spaces",space)
str1 = 'learning pYTHon iS str1 = 'learning pYTHon iS easy'
print("Total no. of vowels",vowels)
easy' str2 = str1.capitalize()
print("Total no. of characters",len(s)-space) str2 = str1.title() print ( str2)
print("Total no. of words",len(s1)) print ( str2)
Q13. What would following expression return? Output Output
1.Word =’amazing’ 2. print(3* "go!") Differentiate Learning Python Is Easy Learning python is easy
>>> Word[:4] , Word[4:] print(5 *"@") between
>>> Word[1:6:2] , print('3'+'7') 1.upper() and isalpha() isalnum()
Word[2:6:2] print('Hello'+ "Student") isupper() The Python isalpha() method returns isalnum() only returns true if a
>>> Word[-7:-3,3] , Word[::- 3. 2.title() and true if a string only contains letters. string contains alphanumeric
2] print("abc" =='abc') capitalize() characters(alphabets or numbers),
>>> Word[::-1] , Word[4:6] print('ABC'=='abc') 3.isalpha() and without symbols.
>>> Word[-3:-1] , Word[-4:-3] print('ABC'!="abc") isalnum() EG: EG:
>>>ord(‘a’),ord(‘z’) 4.strip() and split() str1='abcd' str1='abcd'
>>>ord(‘A’),ord(‘Z’) print(str1.isalpha()) print(str1.isalnum())
>>> chr(65),chr(90) str1 = 'learning pYTHon iS easy' str1 = 'learning pYTHon iS easy'
print(str1.isalpha() ) print(str1.isalnum() )
Upper() Isupper() str1='12abcd' str1='12abcd'
Python upper() method returns Python isupper() method returns print ( str1.isalpha()) print ( str1.isalnum())
str1=' ' str1='12abcd#'
print(str1.isalpha()) print ( str1.isalnum()) 4.
str1='12abcd#' str1=' '
print ( str1.isalpha()) print(str1.isalnum()))
Output Output
True True
False False
False True
False False
False False
Strip() split()
It returns the string after removing split() method in Python split a 3.
the space from the both sides of string into a list of strings after upyHho#4yo
mahender,
the string. It Returns a copy of breaking the given string by the
SINGH,
the string specified separator.
dhoni
EG: EG:
string = " python is easy " text = 'Python is easy'
print(string.strip()) print(text .split())
print(string.strip(' saey ')) word = 'Python: is: easy'
print(word .split(':')) 5.
word = 'CatBatSatFatOr'
NA###5CV###4
print(word .split('t'))
4.
Output Output
##41
python is easy ['Python', 'is', 'easy']
python i ['Python', ' is', ' easy']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']
1.
2. M. S. Dhoni
3 letters found