-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathstringmainpulation.py
More file actions
58 lines (47 loc) · 1.82 KB
/
Copy pathstringmainpulation.py
File metadata and controls
58 lines (47 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Simple string manipulation great for data cleaning and processing for e.g. tokenization or ML models. All methods are self-explanatory:
remove_punctuation removes all punctuation
count_vowels counts the number of vowels in a string and returns a dictionary with count
is_palindrome checks if the given string is a palindrome or not and returns a boolean value
"""
import easyPythonpi as pi
import regex as re
def remove_punctuation(my_str:'str')->'str':
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# remove punctuations from the string
no_punct = ""
#run a for loop and traverse every element in a string and check ,if char is not match with punctuations char then it will add in no_punct
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
return no_punct
def count_vowels(ip_str:'str')->'dict':
# string of vowels
vowels = 'aeiou'
# make it suitable for comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
#return the count dictionary
return count
# To check if the given parameter is palindrome or not
def is_palindrome(x:'str')->'bool':
# convert into string data type so as to iterate through each character
x=str(x)
# remove whitespace, lower case letters, and then remove punctuations
x = x.replace(" ", "")
x = x.lower()
x = remove_punctuation(x)
# Convert original string to its reversed version
r=''
for i in range(len(x)-1,-1,-1):
r=r+x[i]
# if the parameter get matched with its reverse then returns true othewise false
if x==r:
return True
else:
return False