0% found this document useful (0 votes)
6 views4 pages

String Manipulation Techniques in Python

The document provides a collection of Python string manipulation techniques and functions, including comparisons, reversals, palindrome checks, whitespace removal, and character counting. It also covers advanced topics like anagram checks, longest substrings, string rotation, and custom implementations of common string functions. Each section includes code examples demonstrating the respective functionality.

Uploaded by

snehitha2703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

String Manipulation Techniques in Python

The document provides a collection of Python string manipulation techniques and functions, including comparisons, reversals, palindrome checks, whitespace removal, and character counting. It also covers advanced topics like anagram checks, longest substrings, string rotation, and custom implementations of common string functions. Each section includes code examples demonstrating the respective functionality.

Uploaded by

snehitha2703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Difference between `is` and `==`


a = "hello"
b = "hello"

print(a == b) # True (compares values)


print(a is b) # True (same memory object, but not always guaranteed)

- `==` → checks value equality


- `is` → checks memory reference

2. Strings mutable or immutable?


Strings are immutable.

s = "hello"
# s[0] = "H" ■ Error
s = "H" + s[1:]
print(s) # Hello

3. Reverse a string
s = "python"
print(s[::-1]) # nohtyp

# Without slicing
rev = ""
for ch in s:
rev = ch + rev
print(rev) # nohtyp

4. Palindrome check
s = "madam"
print(s == s[::-1]) # True

5. Remove whitespaces
s = " hello world "
print([Link]()) # "hello world"

6. Count occurrences of a character


s = "programming"
print([Link]("m")) # 2

7. First non-repeating character


s = "swiss"
for ch in s:
if [Link](ch) == 1:
print(ch) # w
break

8. Anagram check
s1, s2 = "listen", "silent"
print(sorted(s1) == sorted(s2)) # True

9. Longest substring without repeating characters


s = "abcabcbb"
longest, temp = "", ""
for ch in s:
if ch in temp:
temp = temp[[Link](ch)+1:]
temp += ch
if len(temp) > len(longest):
longest = temp
print(longest) # "abc"

10. Longest common prefix


words = ["flower", "flow", "flight"]
prefix = words[0]
for w in words[1:]:
while not [Link](prefix):
prefix = prefix[:-1]
print(prefix) # "fl"

11. Alternate characters


s = "python"
print(s[::2]) # pto

12. Split without split()


s = "hello world python"
word, words = "", []
for ch in s:
if ch == " ":
[Link](word)
word = ""
else:
word += ch
[Link](word)
print(words) # ['hello', 'world', 'python']

13. Rotate string


s = "abcde"
n = 2
rotated = s[n:] + s[:n]
print(rotated) # cdeab

14. Only digits


s = "12345"
print([Link]()) # True

15. Extract numbers


import re
s = "Order 123, price 45"
print([Link](r"\d+", s)) # ['123', '45']

16. Starts with substring


s = "hello world"
print([Link]("hello")) # True

17. Remove special characters


import re
s = "Hello@# World!!"
print([Link](r'[^A-Za-z0-9 ]', '', s)) # Hello World

18. Most frequent character


s = "programming"
freq = max(set(s), key=[Link])
print(freq) # g

19. String compression


s = "aaabbc"
res, count = "", 1
for i in range(1, len(s)):
if s[i] == s[i-1]:
count += 1
else:
res += s[i-1] + str(count)
count = 1
res += s[-1] + str(count)
print(res) # a3b2c1

20. Implement replace()


def my_replace(s, old, new):
return [Link]([Link](old))

print(my_replace("hello world", "world", "python")) # hello python

21. Rotation check


s1, s2 = "abcde", "cdeab"
print(len(s1) == len(s2) and s2 in (s1+s1)) # True

22. Unique characters without extra DS


s = "abcdef"
unique = True
for i in range(len(s)):
if s[i] in s[i+1:]:
unique = False
break
print(unique) # True

You might also like