0% found this document useful (0 votes)
6 views6 pages

Playing With Strings

Uploaded by

anilkoranga.pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Playing With Strings

Uploaded by

anilkoranga.pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

# PLAYING WITH STRINGS

s="CARPE DIEM"

n=len(s)//2 #5

print(s[:n],s[n:],s[1:n],s[n:len(s)-1]) #CARPE DIEM ARPE DIE

# Creating new string by taking selected characters from a string

ss=""

ss=s[:6]

ss=ss+"MAGIC"

print(ss) #CARPE MAGIC

st="JPS"

ss=ss+st[2]

print(ss) #CARPE MAGICS

st="JayceEs PubliC School"

# Create a new string ss from st which should contain only upper case alphabets from st

ss=""

for i in st:

if i.isupper():

ss=ss+i

print(ss)#JEPCS

#OR using range() function

for i in range (len(st)):

if st[i].upper():

ss=ss+st[i]

# WAP to input any character and print it is in upper or lower case.

#If in lower case print its equivalent upper case alphabet

#If in upper case print its equivalent lower case alphabet

Example:

Enter alphabet: P

it is in Upper

its lower= p
ch=input("Enter alphabet ")

if ch.isupper():

print("It is upper case ")

print("It's Lower= ",ch.lower())

elif ch.islower():

print("It is lower case ")

print("It's Upper= ",ch.upper())

#Above program without using isupper/islower/upper/lower functions

ch=input("Enter alphabet ")

if ch>='A' and ch<='Z':

print(" In Upper")

print(" Its Lower= ",chr(ord(ch)+32))

elif ch>='a' and ch<='z':

print(" In Lower")

print(" Its Upper= ",chr(ord(ch)-32))

else:

print("Not an alphabet")

# WAP to convert each lower case alphabet in string to upper and

# upper case to lower

#if string is "JaYceEs" it should be converted and stored in another

# variable as "jAyCEeS"

st="JaYceEs"

ss=""

for i in st:

if i.isupper():

ss=ss+i.lower()

elif i.islower():

ss=ss+i.upper()

print(ss)
# Predict Output of following for inputs as given (i) str='aabbcc' (ii) str='aaccbb' (iii)str='abcc'

str='aabbcc'

#str='aaccbb'

#str='abcc'

while True:

if str[0]=='a':

str=str[2:]

elif str[-1]=='b':

str=str[:2]

else:

break

print(str)

# Predict Output of following

s='School2@com'

k=len(s)

m=" "

for i in range(0,k):

if(s[i].isupper()):

m=m+s[i].lower()

elif s[i].isalpha():

m=m+s[i].upper()

else:

m=m+'bb'

print(m) #sCHOOLbbbbCOM

# Predict Output of following

Msg1="UeNcPNB"

Msg2="GUeSTs"

Msg3=""

for I in range(0,len(Msg2)+1):#6

if Msg1[I]>="A" and Msg1[I]<="M":

Msg3=Msg3+Msg1[I]
elif Msg1[I]>="N" and Msg1[I]<="Z":

Msg3=Msg3+Msg2[I]

else:

Msg3=Msg3+"*"

print (Msg3) #G*e*TsB

# Predict Output of following

Msg="CompuTer"

Msg1=''

for i in range(0, len(Msg)):

if Msg[i].isupper():

Msg1=Msg1+Msg[i].lower()

elif i%2==0:

Msg1=Msg1+'*'

else:

Msg1=Msg1+Msg[i].upper()

print(Msg1) #cO*P*t*R

#Find the output of the following Python program:

st="sTUdeNT"

newstr = ""

count = 0

for i in st:

if count%2 !=0:

newstr = newstr+ str(count)

elif i.islower():

newstr = newstr+i.upper()

else:

newstr = newstr+i

count +=1

newstr = newstr+st[:1]

print ("The new string is :",newstr ) # S1U3E5Ts


#WAP to input a string and Check It is Palindrome or not

#1. You can use another string

# Palindrome Strings- madam, jahaj, dad, aa

s=input("Enter any string ")

x=len(s)-1 #3

ss=""

for i in range(x,-1,-1):#(3-0)

ss+=s[i]

if ss==s:

print("Palindrome")

else:

print("Not Palindrome ")

#2. Without using another String

s=input("Enter any string ")

x=len(s)-1

for i in range(0,len(s)//2):

if s[i]!=s[x]:

print("Not Palindrome")

break

x-=1

else:

print("Palindrome ")

#Find the output of the following Python program:

Msg1="WeLcOME"

Msg2="GUeSTs"

Msg3=""

for I in range(0,len(Msg2)+1):

if Msg1[I]>="A" and Msg1[I]<="M":

Msg3=Msg3+Msg1[I]

elif Msg1[I]>="N" and Msg1[I]<="Z":

Msg3=Msg3+Msg2[I]
else:

Msg3=Msg3+"*"

print (Msg3)

#Find the output of the following Python program:

str='[email protected]'

#0123456789101112

m=""

for i in range(0,len(str)):#12

if(str[i].isupper()):

m=m+str[i].lower()

elif str[i].islower():

m=m+str[i].upper()

else:

if i%2==0:

m=m+str[i-1]

else:

m=m+"#"

print(m) # fUN#pYTHONn#.

#WAP to input a String and a character and print No of occurrences of the character in string

st=input("Enter String ")

ch=input("Enter Character to search ")

p=0

for i in st:

if ch==i:

p+=1

if p==0:

print(" Character Not found ")

else:

print("Character found ", p,"times")

You might also like