String ===> A string is group of ch.
or sequence of character
str = "Hello World"
print(str[4])
print(str[5])
print(str[-5])
print(str[::2])
print(str[1:8:2])
print(str[1:3])
How to take input in string....
name = input("Enter a name: ")
print(name)
Attribute of string
0. len() ==> return the len of string
str = input("Enter a String : ")
length = len(str)
print("length : " , length)
1. count(str , start , end) ===> Returns number of times substring str occurs in
the given string. If we do not give start index and end index then searching
starts from index 0 and ends at length of the string.
str = "This is a Holiday. bla bla somethings is a odd "
print(str.count("a" , 0, 5))
# 2. lower() ===> convert the uppercase in lowercase
str = "This is a Holiday"
print(str.lower())
# 3. upper() ===> convert the lowercase in uppercase
str = "This is a Holiday"
print(str.upper())
4. title() ==> Returns the string with first letter of every word in the string in
uppercase and rest in lowercase
str = "this is a holiday"
print(str.title())
4. find(str, start, end) ==> Returns the first occurrence of index of substring
str occurring in the given string. If we do not give start and end then searching
starts from index 0 and ends at length of the string. If the substring is not
present in the given string, then the function returns - 1
str = "This is Something"
print(str.find("z"))
5. isupper() ===> check that string is in uppercase or not
str = "This"
print(str.isupper())
6. islower() ===> check that string is in lowercase or not
str = "this"
print(str.islower())
7. startWith(string)
str = "This is holiday "
print(str.startswith("This"))
8. index()
str = "This is holiday"
print(str.index("z"))
traversal on a string...
str = "This is holiday"
for i in range(len(str)):
print(str[i])
print("z" in str )
Q1 Count the number of vowel in a given string
def countVowel(str):
cnt = 0
for i in range(len(str)):
if(str[i] == "a" or str[i] == "e" or str[i] == "i" or str[i] == "o" or
str[i]=="u" or str[i]=="A" or str[i]=="E" or str[i]=="I" or str[i]=="O" or
str[i]=="U" ):
cnt += 1
return cnt
str = "This is Holiday thsisi s nd s s "
ans = countVowel(str)
print(ans)
!Q2. Write a program with a user defined function with string as a parameter which
replaces all vowels in the string with '*'.
def convertString(str):
ans = ""
for i in range(len(str)):
if(str[i] == "a" or str[i] == "e" or str[i] == "i" or str[i] == "o" or
str[i] == "u" or str[i] == "A" or str[i] == "E" or str[i] == "I" or str[i] == "O"
or str[i] == "U"):
ans += "*"
else:
ans +=str[i]
return ans
str = input("Enter the String : ")
newStr = convertString(str)
print(newStr)
Q3. Reverse a string without creating a new string
str = input("Enter the String : ")
for i in range(-1,-len(str)-1,-1):
print(str[i],end="")
! Q4. Write a program which reverses a string passed as parameter and stores the
reversed string in a new string. Use a user defined function for reversing the
string.
def reverseStr(str):
newStr = ""
for i in range(-1,-len(str)-1,-1):
newStr +=str[i]
return newStr
str = input("Enter the string : ")
ans = reverseStr(str)
print("Original String : ", str)
print("Reversed String : ", ans)
!Q5. Write a program using a user defined function to check if a string is a
palindrome or not. (A string is called palindrome if it reads same backwards as
forward. For example, Kanak is a palindrome.)
def checkPalimdrom(str):
newStr = ""
for i in range(-1,-len(str)-1,-1):
newStr +=str[i]
return newStr
str = input("Enter the string : ")
ans = checkPalimdrom(str)
if(str==ans):
print("Given string is palimdrom : ")
else:
print("Given string is not a palimdrom ")
or
def checkPalimdrom(str):
i = 0
j= len(str)-1
while(i<=j):
if(str[i]!=str[j]):
return False
i+=1
j-=1
return True
str = input("Enter a string : ")
ans = checkPalimdrom(str)
if(ans):
print("Given string is palimdrom : ")
else:
print("Given string is not a palimdrom ")
def fact(n):
if n<=1:
return 1
else:
return (n*fact(n-1))
num = int(input("Enter a number : "))
ans = fact(num)
print(ans)
num = int(input("Enter a number: "))
ans = 1
for i in range(1, num+1):
ans = ans*i
print(ans)
0 1 1 2 3 5 8
def fib(n):
if(n==0 or n==1):
return 1
else:
return(fib(n-1)+fib(n-2))
ans = fib(5)
print(ans)