🐍 10 Essential String Operations in Python (With Examples & Output)

 

Working with strings is one of the most common tasks in programming. Whether you're preparing for coding interviews or brushing up on Python basics, mastering string operations is a must.

Here are 10 essential string problems solved in Python, along with clear explanations and example outputs.


✅ 1. Reverse a String

str1='shami' rev_str=str1[::-1] print(rev_str)

Explanation:
Using Python’s slicing feature [::-1], we reverse the string.

Output:

imahs

✅ 2. Check if Two Strings Are Anagrams

def are_anagrams(s1, s2): return sorted(s1)==sorted(s2) print(are_anagrams("listen","silent"))

Explanation:
Two strings are anagrams if they have the same characters in any order. Sorting helps compare them.

Output:

True

✅ 3. Find the First Non-Repeating Character in a String

def non_repating_str(s): for char in s: if s.count(char)==1: return char print(non_repating_str("aabbssdhsmi"))

Explanation:
The loop checks each character’s count. The first character with count 1 is returned.

Output:

d

✅ 4. Count the Frequency of Each Character

s = "hello world" freq = {} for ch in s: freq[ch] = freq.get(ch, 0) + 1 print(freq)

Explanation:
Using a dictionary, we count how many times each character appears.

Output:

{'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}

✅ 5. Remove All Vowels from a String

def remove_vowels(str4): vowels = "aeiouAEIOU" result = "" for char in str4: if char not in vowels: result += char return result print(remove_vowels("Mohd Shami"))

Explanation:
We iterate through the string and skip vowels.

Output:

Mhd Shm

✅ 6. Replace a Substring in a String

str5 = "Shami is a great player" str6 = "Sammy" result = str5.replace("Shami", str6) print(result)

Explanation:
Using .replace(), we substitute "Shami" with "Sammy".

Output:

Sammy is a great player

✅ 7. Check if a String is a Valid Palindrome

str7="shami" str8="imahs" str9=str8[::-1] if str8==str9: print("The strings are plindrome.") else: print("The strings are not plindrome.")

Explanation:
The code checks if str8 is equal to its reverse. Small typo: "plindrome" should be "palindrome".

Output:

The strings are not plindrome.

✅ 8. Find the Longest Substring Without Repeating Characters

s = "abcabcbb" longest = "" current = "" for char in s: if char in current: current = current[current.index(char)+1:] current += char if len(current) > len(longest): longest = current print("Longest substring without repeating characters:", longest) print("Length:", len(longest))

Explanation:
This uses a sliding window-like approach to track the longest sequence of unique characters.

Output:

Longest substring without repeating characters: abc Length: 3

✅ 9. Implement the strstr() Function

def strstr(haystack, needle): if needle == "": return 0 # As per C/C++ behavior for i in range(len(haystack) - len(needle) + 1): if haystack[i:i+len(needle)] == needle: return i return -1

Explanation:
This mimics C’s strstr() function, finding the first occurrence of needle in haystack.

Example:

print(strstr("hello", "lo")) # Output: 3

✅ 10. Check if a String Has All Unique Characters

def is_unique(s): for char in s: if s.count(char) > 1: return False return True # Test cases print(is_unique("abcdef")) # True print(is_unique("hello")) # False

Explanation:
The function checks if any character appears more than once.

Output:

True False

🎯 Final Thoughts

These 10 string problems cover:

  • Reversal

  • Searching

  • Replacing

  • Frequency analysis

  • Palindrome & anagram logic

  • Substring and uniqueness detection

They’re excellent for coding practice, interviews, or improving your understanding of string operations in Python.

Comments