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

Career Enhancement Topic 2 String

Uploaded by

vashutyagi761
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)
17 views4 pages

Career Enhancement Topic 2 String

Uploaded by

vashutyagi761
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
You are on page 1/ 4

1.

Reversing a String

To reverse a string in Python, you can use slicing:

Code-

s = "hello"
reversed_s = s[::-1]
print(reversed_s)

# Output : "olleh"

2. Checking for a Palindrome

A palindrome is a string that reads the same backward as forward.

Code-

def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam"))

# Output: True

def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("hello"))

# Output: False

3. Counting the Occurrence of Characters

You can use Python's collections.Counter to count character occurrences.

Code-
from collections import Counter
s = "abracadabra"
char_count = Counter(s)
print(char_count)

# Output: {'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}

4. Checking if Two Strings are Anagrams

An anagram is when two strings have the same characters in a different order.

Code-

def are_anagrams(s1, s2):


return sorted(s1) == sorted(s2)

print(are_anagrams("listen", "silent")) # Output: True


print(are_anagrams("hello", "world")) # Output: False

5. Finding the Longest Common Substring

You can use dynamic programming to find the longest common substring between two strings.

Code-

def longest_common_substring(s1, s2):


m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
length, end = 0, 0

for i in range(1, m + 1):


for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
if dp[i][j] > length:
length = dp[i][j]
end = i
return s1[end - length: end]

print(longest_common_substring("abcde", "abfde"))
# Output: "ab"

6. Splitting and Joining Strings

To split a string into a list of substrings based on a delimiter, use split(). To join a list of
strings back into a single string, use join().

Code-

s = "apple,banana,cherry"
split_s = s.split(",")
print(split_s) # Output: ['apple', 'banana', 'cherry']

joined_s = "-".join(split_s)
print(joined_s) # Output: "apple-banana-cherry"

7. Finding Substring Occurrences

To find all occurrences of a substring in a string, you can use find() or a loop:

Code-

s = "abracadabra"
substring = "abra"
index = s.find(substring)
while index != -1:
print(f"Found at index: {index}")
index = s.find(substring, index + 1)

8. Replacing Substrings

You can replace all occurrences of a substring using the replace() method.

Code-

s = "abracadabra"
new_s = s.replace("abra", "xyz")
print(new_s)

# Output: "xyzcadxyz"

9. Checking for Substring

To check if a string contains another string, use the in keyword.

Code-

s = "hello world"
print("world" in s) # Output: True
print("python" in s) # Output: False

You might also like