Python program to toggle each character in a string
Toggle each character in a string
The string is a combination of characters, but in programing language like python, a string is an independent datatype that can be treated as character or string both because in python string of length 1 is also a string, not character. In this python program, we will check the type of string available on the basis of their case
So Today we will se program to Toggle each character in a string
Algorithm
- Step 1:- Start.
- Step 2:- Take user input as string.
- Step 3:- Initialize another Empty String as String1.
- Step 4:- Start iterating through string.
- Step 5:- Check for the case of each iterator.
- Step 6:- Change case of each iterator.
- Step 7:- Concatenation each element to String1 after changing it’s case.
- Step 8:- Print the String1.
- Step 9:- End.
Python program to change case of the String
Run
#take user input String = 'GuDDuBHaiyA' #initialize other empty String String1 = str() #iterate through String for i in String: #check the case of each iterator if i.isupper(): #change it to opposit case i = i.lower() #Concat each iterator to String1 String1 = String1 + i else: i = i.upper() String1 = String1 + i #print String1 print(String + ' after changing ' + String1)
GuDDuBHaiyA after changing gUddUbhAIYa
Method 2
Program to toggle each character in a string using standard library functions is given below.
Run
# Python program to toggle each character in a string str = 'Root' print("String after the characters are toggled : ",str.swapcase())
String after the characters are toggled : rOOT
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
input=’CHowDEswARi’
output=””
for i in input:
ans=ord(i)
if ans>=65 and ans<=90:
output+=chr(ans+32)
else:
output+=chr(ans-32)
print(input + ' after changing ' + output)
stg = “”
for i in input(“Enter a String – “):
if i.isupper():
stg+=i.lower()
else:
stg+=i.upper()
print(stg)
# Program toggle each char in a string
c=input(“Enter a string : “)
s=”
for i in c:
if i.isupper():
i=i.lower()
s+=” “.join(i)
else:
i=i.upper()
s+=” “.join(i)
print(s)
str=input()
l1=list(str)
l2=[]
# print(l1)
for i in str:
# print(i,end=”)
if ord(i) in range(65,90):
l2.append(i.casefold())
if ord(i) in range(97,123):
l2.append(i.upper())
# print(l2)
for i in l2:
print(i,end=”)
a=”rOOt”
s=””
for i in a:
if i.islower():
s+=i.upper()
else:
s+=i.lower()
print(s)
str1=str(input())
str2=str1.swapcase()
print(str2)
#more easier way in python is
string1=input(“Enter the String:”)
string2=string1.swapcase()
print(“{} after changinh {}”.format(string1,string2))
print(string.swapcase())
two lines code:-
s=str(input())
print(n.swapcase())
st=input(“Enter the string : “)
print(st.swapcase())
raw = input()
res_str = “”
for i in raw:
if i.isupper() == True:
res_str += i.lower()
else:
res_str += i.upper()
print(res_str)
a = input()
print(a.swapcase())