Please click on ads

Please click on ads

Friday, 5 March 2021

python codes with question

Write a while loop that starts at the last character in thestring and works its way backwards to the first character in the string,printing each letter on a separate line, except backwards

index=0
p='banana'
while index<len(p):
index=index-1
s=p[index]
print(s)

Given that fruit is a string, what does fruit[:] mean?
s='fruit'
print(s[:])

#Writecodein a function named count, to accepts astring and the letter as arguments.Find the number of times the lettrs appear in the string.
p=input("Enter string:")
q=input("Enter character to check:")

def check(p,q):
if not p:
return 0
elif p[0]==q:
return 1+check(p[1:],q)
else:
return check(p[1:],q)
print("Count is:")
print(check(p,q))


#Write a program that reads a string from the user and uses a loop to determines whether or not it is a palindrome. Display the result, including a meaningful output message. [A string is a palindrome if it is identical forward and backward.]
x = input('enter the string')

w = ''
for i in x:
w = i + w

if (x == w):
print("Yes")
else:
print("No")


string = 'bat cat'
print(string.endswith(('txt', 'cat', 'java','ant', 'orld')))



a='vit is a branch of diploma'
print('the original string is :'+a)
res=[]
for i in a:
if i in 'a e i o u A E I O U':
res.extend(i)
res = ''.join(res)
print('string after consonents :'+str(res))









ake the following Python code that stores a string:str = 'X-DSPAM-Confidence:0.8475'Use find and string slicing to extract the portion of the string after thecolon character and then use the float function to convert the extractedstring into a floating point number.
str='X-DSPAM-Confidence:0.8475'
print('the length of the String is :')
print(len(str))
p=str.find(':')
print('the index of : is')
print(p)
print('the string before the colon is :')
print(str[0:18])
print('the string after the colon is :')
q=float('0.8475')
print(q)




Write a program to find the number of vowels, consonents, digits and white space characters in a string.
def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i'
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
elif (ch >= '0' and ch <= '9'):
digit += 1
else:
specialChar += 1
print("Vowels:", vowels)
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)
str = "vit is a number 1"
countCharacterType(str)




Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Avul Pakir Jainulabdeen Abdul Kalam, then the output should be A.P.J.A.Kalam.
def name(s):
l = s.split()
new = ""

for i in range(len(l)-1):
s = l[i]

new += (s[0].upper()+'.')


new += l[-1].title()

return new


s ="Avul Pakir Jainulabdeen Abdul Kalam"
print(name(s))



How to check if string ends with one of the strings from a list?
string = 'python'
print(string.endswith(('txt', 'thon', 'on','bat', 'orld')))









Write a program to make a new string with all the consonents deleted from the string "Hello, have a good day".
a =input('enter the string')


# printing original string
print("The original string is : " + a)

# Remove all consonents from string
# Using loop
res = []
for i in a:
if i in "aeiouAEIOU":
res.extend(i)
res = "".join(res)

# printing result
print("String after consonents removal : " + str(res))

No comments:

Post a Comment