- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-String-Functions.py
More file actions
Latest commit
102 lines (77 loc) · 2.07 KB
/
Copy path06-String-Functions.py
File metadata and controls
102 lines (77 loc) · 2.07 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
101
102
name='xyze'
name="xyze"
name='''xyze'''
name="""xyze"""
print(name) # xyz
# Accessing Characters from String
print('\nFirst Char : ',name[0]) # x
print('Second Char : ',name[1]) # y
print('Last Char : ',name[-1]) # e
# Length of String
print('\nLength : ',len(name)) # 4
# String Slicing
print('\nSlicing String :',name[0:5]) # xy
print('Slicing String :',name[:2]) # xyz
print('Slicing String :',name[2:]) # ze
# String Concatenation
name1='xyz'
name2='abc'
name3=name1+name2
print('\nConcatenation : ',name3) # xyzabc
# String Repetition
name='xyz'
name=name*3
print('\nRepetition : ',name) # xyzxyzxyz
# String Transformation
# Lowercase
name='XYZ'
name=name.lower()
print('\nLowercase : ',name) # xyz
# Uppercase
name='xyz'
name=name.upper()
print('\nUppercase : ',name) # XYZ
# Titlecase
name='xyz abc'
name=name.title()
print('\nTitlecase : ',name) # Xyz Abc
# Capitalize
name='xyz abc'
name=name.capitalize()
print('\nCapitalize : ',name) # Xyz abc
# Swapcase
name='xyz ABC'
name=name.swapcase()
print('\nSwapcase : ',name) # XYZ abc
# String Stripping
name='\n\n\n\n\n\n xyz '
name=name.strip()
print('\nStrip : ',name) # xyz
# String Replacing
name='xyz abc'
name=name.replace('abc','123')
print('\nReplace : ',name) # xyz 123
'''-------------------------------------------------------------------'''
# String Splitting
name='xyz abc'
name=name.split(' ')
print('\nSplit : ',name) # ['xyz', 'abc']
# String Joining
name= ['xyz', 'abc']
name=' '.join(name)
print('\nJoin : ',name) # xyz abc
# String Formatting
name='xyz'
age=20
name=f'{name} is {age} years old'
print('\nFormat : ',name) # xyz is 20 years old
# String Formatting with format()
name='xyz'
age=20
name='{} is {} years old'.format(name, age)
print('\nFormat : ',name) # xyz is 20 years old e4rl5t6789
# String Formatting with % operator
name='xyz'
age=20
name='%s is %d years old'% (name, age)
print('\nFormat : ',name) # xyz is 20 years old