-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.py
More file actions
72 lines (61 loc) · 1.98 KB
/
Copy pathString.py
File metadata and controls
72 lines (61 loc) · 1.98 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import string
import random
# Sort Words in Alphabetic Order
print('Sort Words in Alphabetic Order:')
my_str = 'I am Mohammad and I am a python developer'
my_str = my_str.lower().split(' ')
my_str.sort()
print(my_str)
print('-----------------------')
# Remove Punctuation from a String
print('Remove Punctuation from a String')
punctuationStr = 'hello ...... this is encrypterd @#$%&*'
# First way
punctuation = '''''!()-[]{};:'"\,<>./?@#$%^&*_~'''
output = ''
for char in punctuationStr:
if char not in punctuation:
output = output + char
# Second way
puntuation = string.punctuation
punctuationStr = punctuationStr.translate(str.maketrans('', '', puntuation))
print('input is: ', punctuationStr)
print('output is: ', output)
print('-----------------------')
# reverse a string
print('reverse a string')
firstStr = 'Java Developer'
reversedStr = ''
for char in firstStr:
reversedStr = char + reversedStr
# OR
reversedStr = firstStr[::-1]
print('input is: ', firstStr)
print('output is: ', reversedStr)
print('-----------------------')
# Convert List to String in Python
print('Convert List to String in Python:')
first_list = ["Python", "Convert", 11, "List", 12, "String", "Method"]
# to print int values, you should conver int to str with map
convertedStr = " ".join(map(str, first_list))
print('input is: ', first_list)
print('output is: ', convertedStr)
print('-----------------------')
# concatenate two strings in Python
print('concatenate two strings in Python:')
str1 = "Hi"
str2 = 'Mohammad'
print('output is: ', " ".join([str1, str2]))
print('-----------------------')
# generate a Random String
print('generate a Random String')
length = 10
ranStr = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
print('The randomly generated string is:', ranStr)
print('-----------------------')
# convert Bytes to string
print('convert Bytes to string:')
byteData = b"Lets eat a \xf0\x9f\x8d\x95!"
strData = byteData.decode('UTF-8')
print(strData)
print('-----------------------')