string_data=input("Enter a string:")vowels={'a','e','i','o','u'}# Using Count() Methodlist1=[]forchinstring_data:ifchinvowels:ifchnotinlist1:list1.append(ch)print("{} occurred {} times".format(ch,string_data.count(ch)))
Without using count() method:
string_data=input("Enter a string:")vowels={'a','e','i','o','u'}d={}forchinstring_data:ifchinvowels:d[ch]=d.get(ch,0)+1print(d)forkey,valueinsorted(d.items()):print(key,"occurred",value,"times")
string_data=input("Enter string: ")d={}forxinstring_data:d[x]=d.get(x,0)+1print(d)print(d.items())# for meaningful outputforkey,valueinsorted(d.items()):print(key,"occurred",value,"times")
Question by Subscriber:
How to ignore special characters like ? , . etc
If you want to ignore special characters and focus on the alphabets and digits only, then you can do that by some extra work
You can either import string class to get a set of all characters that you need to focus on, or you can manually add the set of characters you particularly want to focus on.
we will name them as valid_characters Here goes the code:
Assume input string contains only alphabet symbols and digits.
We need to write a program to sort characters of the string were we will sort alphabets first and then digits will follow
Sample Input: C8N9A2U6R5B7
Output: ABCNRU256789
string_data=input("Enter some alphanumeric string to sort: ")alphabets=[]digits=[]forchinstring_data:ifch.isalpha():alphabets.append(ch)else:digits.append(ch)result=''.join(sorted(alphabets)+sorted(digits))print("You entered: ",string_data)print("Final result after Sorting: \n",result)
Let us understand the two scenarios of this problem
Scenario1: If both the strings are of same length:
string1=input("Enter first string: ")string2=input("Enter second string: ")i,j=0,0# object initialization using Tuple Unpackingresult=""whilei<len(string1)orj<len(string2):result=result+string1[i]+string2[j]i=i+1j=j+1print("You entered first string : ",string1)print("You entered second string : ",string2)print("Final result after alternate merge: \n",result)
Scenario 2: If the strings have different length:
string1=input("Enter first string: ")string2=input("Enter second string: ")i,j=0,0# object initialization using Tuple Unpackingresult=""whilei<len(string1)orj<len(string2):ifi<len(string1):result=result+string1[i]i=i+1ifj<len(string2):result=result+string2[j]j=j+1print("You entered first string: ",string1)print("You entered second string: ",string2)print("Final result after alternate merge: \n",result)
string_data=input("Enter a string: ")print("Characters present at even index position: ")i=0whilei<len(string_data):print(string_data[i])i=i+2print("Characters present at odd index position: ")i=1whilei<len(string_data):print(string_data[i])i=i+2
2nd Way:
string_data=input("Enter a string: ")print("Characters present at even index position: ")print(string_data[::2])# orprint(string_data[0::2])print("Characters present at odd index position: ")print(string_data[1::2])
Sample Input: one two three four five six seven eight
Output: one owt three ruof five xis seven thgie
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string_data=input("Enter a string: ")list1=string_data.split()print(list1)list2=[]i=0whilei<len(list1):ifi%2==0:list2.append(list1[i])else:list2.append(list1[i][::-1])# Reversing word and then adding to list2i=i+1print(list2)result=" ".join(list2)print("The string you entered:",string_data,sep="\n")print("The result after reversing order of words in the string: ",result,sep="\n")
string_data=input("Enter a string: ")list1=string_data.split()print(list1)list2=[]forwordinlist1:list2.append(word[::-1])# Reversing word and then adding to list2print(list2)result=" ".join(list2)print("The string you entered:",string_data,sep="\n")print("The result after reversing order of words in the string: ",result,sep="\n")
Sample Input: Hello everyone ! Welcome to MV's Code Guide
Output: Guide Code MV's to Welcome ! everyone Hello
Using Slice operator:
1
2
3
4
5
6
7
8
string_data=input("Enter a string: ")list1=string_data.split()#Using Slice Operator to reverse the items of list1list2=list1[::-1]# Using join() to form a string from items of list2 result=" ".join(list2)print("The string you entered:",string_data,sep="\n")print("The result after reversing order of words in the string: ",result,sep="\n")
Using While loop:
1
2
3
4
5
6
7
8
9
10
11
12
string_data=input("Enter a string: ")list1=string_data.split()print(list1)list2=[]i=len(list1)-1whilei>=0:list2.append(list1[i])i=i-1print(list2)result=" ".join(list2)print("The string you entered:",string_data,sep="\n")print("The result after reversing order of words in the string: ",result,sep="\n")
string_data=input("Enter data you want to reverse")result=string_data[::-1]print("The string data you entered is :",string_data)print("The reversed string is:",result)
2nd way: Using reversed() function:
Python provides inbuilt function reversed()
It returns reversed object and you can use join() method of strings to get your required output form reversed object.
1
2
3
4
5
6
7
string_data=input("Enter data you want to reverse")reversed_object=reversed(string_data)print("The string data you entered is :",string_data)forcharacterinreversed_object:print(character,end='')print()print(type(reversed_object))
3rd way : Using While loop:
1
2
3
4
5
6
7
8
string_data=input("Enter data you want to reverse: ")result=""i=len(string_data)-1whilei>=0:result=result+string_data[i]i=i-1print("The string data you entered is :",string_data)print("String data after reversal: ",result)