DEV Community

Sowntharya
Sowntharya

Posted on

Python String Programs

The Python str (string) data type is used to store and manipulate text. Beyond basic string operations, Python provides powerful methods that help developers validate, transform, search and process text efficiently.

1. Count the Frequency of Each Character

text = "python programming"

frequency = {}

for char in text:
    if char != " ":
        frequency[char] = frequency.get(char, 0) + 1

print(frequency)
Enter fullscreen mode Exit fullscreen mode

Output
{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 2,
'g': 2, 'r': 2, 'a': 1, 'm': 2, 'i': 1}

Short explanation

  • frequency = {} creates an empty dictionary.
  • The for loop checks each character in the string.
  • get(char, 0) retrieves the current count or returns 0 if the character is new.
  • Each character's count is increased by 1.
  • Spaces are ignored. Concepts: Strings, loops, dictionaries and get() method.

2. Check Whether a String Is a Palindrome

text = "Madam"

text = text.lower()

if text == text[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")

Enter fullscreen mode Exit fullscreen mode

Output
Palindrome

Short explanation

  • lower() converts the string into lowercase.
  • [::-1] reverses the string using slicing.
  • The original string and reversed string are compared.
  • If both are equal, it is a palindrome. Concepts: String slicing, comparison and case conversion.

3. Remove Duplicate Characters from a String

text = "programming"

result = ""

for char in text:
    if char not in result:
        result += char

print(result)

Enter fullscreen mode Exit fullscreen mode

Output
progamin

Short explanation

  • An empty string is created to store the result.
  • The loop checks each character.
  • If the character is not already present in result, it is added.
  • Duplicate characters are skipped. Concepts: String membership (in), loops and string concatenation.

4. Find the Most Frequent Word in a Sentence

sentence = "python is easy python is powerful python"

words = sentence.split()

frequency = {}

for word in words:
    frequency[word] = frequency.get(word, 0) + 1

most_frequent = max(frequency, key=frequency.get)

print("Most frequent word:", most_frequent)
print("Count:", frequency[most_frequent])

Enter fullscreen mode Exit fullscreen mode

Output
Most frequent word: python
Count: 3

Short explanation

  • split() converts the sentence into a list of words.
  • A dictionary stores each word and its frequency.
  • max(frequency, key=frequency.get) finds the word with the highest count.
  • The final result displays the most frequent word and its count. Concepts: split(), dictionaries, max() and key functions.

5. Check Whether Two Strings Are Anagrams

str1 = "listen"
str2 = "silent"

str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()

if sorted(str1) == sorted(str2):
    print("Anagram")
else:
    print("Not an anagram")

Enter fullscreen mode Exit fullscreen mode

Output
Anagram

Short explanation

  • replace() removes spaces.
  • lower() converts both strings into lowercase.
  • sorted() sorts the characters alphabetically.
  • If both sorted strings are equal, they contain the same characters and are anagrams. Concepts: replace(), lower(), sorted() and string comparison.

Top comments (0)