Table of Contents
hide
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 whether the String is Palindrome.
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
--------------- OR ---------------
def Palindrome(str):
str = str.lower().replace(" ", "") # Ignore case and spaces
return str == str[::-1]
Val=(Palindrome("Madam"))
if Val==True:
print("String is Palindrome")
else:
print("String is not Palindrome")
Output:
String is Palindrome
Example : A Python program to display the Output in a Formatted Form of Sentence/String.
name = "Modi"
age = 75
text = f"Our Prime Minister Name is {name} and He is {age} Years Old."
print(text)
Output:
Our Prime Minister Name is Modi and He is 75 Years Old.
Example : A Python program to display the Index Value of a Searched Word in a Sentence/String.
text = "Hello India"
index = text.find("India")
print(index)
Output:
6
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 Split the String/Sentence into the No. of Words Separately.
text = "Hello World India Welcome in Python World."
words = text.split()
print(words)
Output:
['Hello', 'World', 'India', 'Welcome', 'in', 'Python', 'World.']
Example : A Python program to Combine the Words of a String into a Single String/Sentence.
words = ["Hello", "World", "India", "Welcome","in","Python","World"]
combined_text = " ".join(words)
print(combined_text)
Output:
Hello World India Welcome in Python World
Example : A Python program to check if a String Starts or Ends with a Specific Substring in a Sentence.
text = "Hello, India. India is Great Country"
print(text.startswith("Hello"))
print(text.endswith("Great"))
Output:
True
False
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 display the Largest Word from a Sentence.
text = "Hello World India Welcome in Python World."
words = text.split()
longest = max(words, key=len)
print(longest)
Output:
Welcome
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 Count the Occurrence of Specific Characters in a Sentence.
text = "Hello World India"
char = "l"
print(text.count(char))
Output:
3
Example : A Python program to convert a string into Uppercase and Lowercase Letters.
str = "Indian"
print(str.upper())
print(str.lower())
Output:
INDIAN
indian
Example : A Python program to Capitalize the First Letter of a String/Sentence.
text = "hello world india"
val = text.title()
print(val)
Output:
Hello World India
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 Digit 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 Alphabets/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 check the String is Only Alphanumeric.
str = "Hello123"
val=(str.isalnum())
if val==True:
print("String is Purely Alphanumeric")
else:
print("String is either Digits or Purely Alphabets")
Output:
String is Purely Alphanumeric
--------------- OR ---------------
str = "Python version 3.10"
val=(str.isalnum())
if val==True:
print("String is Purely Alphanumeric")
else:
print("String is either Digits or Alphabets")
Output:
String is either Digits or Alphabets
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 Remove White Space from the String.
text = " Hello World "
textmodified = text.strip()
print(textmodified)
Output:
Hello World
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 whether the Two Strings is Anagrams (Sorted value is the 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 whether 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
0 Comments