-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.py
More file actions
100 lines (53 loc) · 1.64 KB
/
strings.py
File metadata and controls
100 lines (53 loc) · 1.64 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# strings are being surrounded by single or double quotes just like in javascript
name = 'Abdul'
age = 20
# concactinating
# variables are being concactinated with the plus(+) sign just like in javascript
print('Hello my name is ' + name)
print('Hello my name is ' + name + ' and i am ' + str(age) + ' years old')
'''
print('Hello my name is ' + name + ' and i am ' + age + ' years old')
the codes above is going to throw an error because only strings can be concactinated unless we convert the int variable into
a string like the code below
print('Hello my name is ' + name + ' and i am ' + str(age) + ' years old')
'''
#string formating
#areguments by position
'''
we can also contactinate strings and int together by using the format method
like the code below
'''
print('My name is {name} and i am {age}'.format(name=name, age=age))
# or
print(f'My name is {name} and i am {age}')
#String Methods
n = 'abdul frfr'
#capitalize string
print(n.capitalize())
#uppercase
print(n.upper())
#lowercase
print(n.lower())
# swapcase
print(n.swapcase())
#get length
print(len(n))
#replace string
print(n.replace('abdul', 'everyone'))
#count a letter
sub = 'r'
print(n.count(sub))
#to check if it starts with a particular word
print(n.startswith('hello'))
#to check if it ends with a letter or a word
print(n.endswith('v'))
#split the strings which is going to turn it into n array
print(n.split())
#find position of a character or a string
print(n.find('r'))
#check if your variable is alphanumeric
print(n.isalnum())
#check if your variable is only alphabets
print(n.isalpha())
#check if your variable is all numbers
print(n.isnumeric())