Example : A Python program to Reverse each character of the String word.
str = "Welcome"
print(str[::-1]) 

Output:
emocleW
Example : A Python program to Reverse each word of the String Sentence.
str = "Hello India and Indian"
print(" ".join(str.split()[::-1])) 

Output:
Indian and India Hello
Example : A Python program to check the String is Palindrome or Not.
str = "madam"
print(str == str[::-1])

Output:
True

---------------  OR  ---------------

str = "madam"
val=(str == str[::-1]) 
if val==1:
    print("String is Palindrome")
else:
    print("String is not Palindrome")

Output:
String is Palindrome

---------------  OR  ---------------

str = "madam"
val=(str == str[::-1]) 
if val==True:
    print("String is Palindrome")
else:
    print("String is not Palindrome")

Output:
String is Palindrome

---------------  OR  ---------------

str = "Madam"
val=(str == str[::-1]) 
if val==True:
    print("String is Palindrome")
else:
    print("String is not Palindrome")

Output:
String is not Palindrome
Example : A Python program to Count the no. of each Character present in a Sentence/String.
str = "Python is a dominating programming language"
count=(len(str)) 
print("Total no. of Characters are = ",count)

Output:
Total no. of Characters are =  43
Example : A Python program to Count the no. of Words in a Sentence.
str = "Python is awesome programming language"
count=(len(str.split())) 
print("Total words are = ",count)

Output:
Total words are = 5
Example : A Python program to Count the Frequency of Each Character in a Sentence.
from collections import Counter
str = "Hello India"
print(Counter(str)) 

Output:
Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1, ' ': 1, 'I': 1, 'n': 1, 'd': 1, 'i': 1, 'a': 1})
Example : A Python program to convert string into Uppercase and Lowercase Letters.
str = "Indian"
print(str.upper())  
print(str.lower()) 

Output:
INDIAN
indian
Example : A Python program to Replace a Specific Substring from a String.
str = "I prefer Java programming than others"
print(str.replace("Java", "Python"))  

Output:
I prefer Python programming than others
Example : A Python program to check the String is Only/Purely Numeric.
str = "12345"
val=(str.isdigit()) 
if val==True:
    print("String is Purely Numeric")
else:
    print("String is not Purely Numeric")

Output:
String is Purely Numeric
Example : A Python program to check the String is Only/Purely Alphabets.
str = "Python"
val=(str.isalpha()) 
if val==True:
    print("String is Purely Alphabet")
else:
    print("String is not Purely Alphabet")

Output:
String is Purely Alphabet

---------------  OR  ---------------

str = "Python version 3.10"
val=(str.isalpha()) 
if val==True:
    print("String is Purely Alphabet")
else:
    print("String is not Purely Alphabet")

Output:
String is not Purely Alphabet
Example : A Python program to Remove Any Space from the String.
str = "Python is Future Programming Language"
print(str.replace(" ", ""))

Output:
PythonisFutureProgrammingLanguage
Example : A Python program to Swap Each Character from the String.
s = "Hello India"
print(s.swapcase()) 

Output:
hELLO iNDIA
Example : A Python program to Check the Two String is Anagrams (Sorted value is Same) or Not.
s1, s2 = "listen", "silent"

print(sorted(s1))
print(sorted(s2))
print()

val=(sorted(s1) == sorted(s2))
if val==True:
    print("String is Anagrams")
else:
    print("String is Not Anagrams")

Output:
['e', 'i', 'l', 'n', 's', 't']
['e', 'i', 'l', 'n', 's', 't']

String is Anagrams
Example : A Python program to Check the String is Pangram (String contains All English Alphabets/Letters) or Not.
import string

str = "The quick brown fox jumps over the lazy dog"
val=(set(string.ascii_lowercase).issubset(set(str.lower()))) 
if val==True:
    print("String is Pangram")
else:
    print("String is Not Pangram")

Output:
String is Pangram
Example : A Python program to display the most frequent character used/present in a String.
from collections import Counter
str = "banana"
print("The most frequent character in this string is = ",(Counter(str).most_common(1)[0][0]))

Output:
The most frequent character in this string is =  a

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.