-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings
224 lines (121 loc) · 5.49 KB
/
Strings
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
In Python, strings are a sequence of characters, and Python provides a wide range of methods and functions to manipulate them.
Here's a comprehensive guide to string operations, methods, and functions:
String Creation and Basics
# Strings can be created using single, double, or triple quotes
single_quoted = 'Hello'
double_quoted = "Hello"
triple_quoted = '''Hello'''
triple_quoted_multiline = '''This is a
multiline string'''
String Operators
Concatenation (+): Combines two strings.
result = 'Hello' + ' ' + 'World'
Repetition (*): Repeats a string multiple times.
result = 'Hi!' * 3 # Output: 'Hi!Hi!Hi!'
Membership (in / not in): Checks if a substring is in a string.
'H' in 'Hello' # True
'z' not in 'Hello' # True
String Methods
1. Basic Methods
len(string): Returns the length of the string.
len('Hello') # Output: 5
str(): Converts any data type to a string.
str(123) # Output: '123'
2. Case Manipulation
lower(): Converts all characters to lowercase.
'Hello'.lower() # Output: 'hello'
upper(): Converts all characters to uppercase.
'Hello'.upper() # Output: 'HELLO'
capitalize(): Converts the first character to uppercase.
'hello'.capitalize() # Output: 'Hello'
title(): Converts the first character of each word to uppercase.
'hello world'.title() # Output: 'Hello World'
swapcase(): Swaps the case of all characters.
'Hello World'.swapcase() # Output: 'hELLO wORLD'
3. Whitespace Removal
strip(): Removes leading and trailing whitespace.
' Hello '.strip() # Output: 'Hello'
lstrip(): Removes leading whitespace.
' Hello'.lstrip() # Output: 'Hello'
rstrip(): Removes trailing whitespace.
'Hello '.rstrip() # Output: 'Hello'
4. Searching and Replacing
find(substring): Returns the index of the first occurrence of substring, or -1 if not found.
'Hello'.find('e') # Output: 1
rfind(substring): Returns the index of the last occurrence of substring.
'Hello'.rfind('l') # Output: 3
index(substring): Like find(), but raises a ValueError if not found.
'Hello'.index('e') # Output: 1
replace(old, new): Replaces all occurrences of old with new.
'Hello World'.replace('World', 'Python') # Output: 'Hello Python'
5. String Splitting and Joining
split(separator): Splits the string into a list of substrings.
'Hello World'.split() # Output: ['Hello', 'World']
rsplit(separator): Splits from the right.
'a,b,c'.rsplit(',', 1) # Output: ['a,b', 'c']
splitlines(): Splits the string at line breaks (\n).
'Line1\nLine2\nLine3'.splitlines() # Output: ['Line1', 'Line2', 'Line3']
join(iterable): Joins the elements of an iterable (e.g., list) into a single string, using the string as a separator.
','.join(['a', 'b', 'c']) # Output: 'a,b,c'
6. Character Checks
These methods return True or False based on the string's content:
isalpha(): Checks if all characters are alphabetic.
'Hello'.isalpha() # Output: True
isdigit(): Checks if all characters are digits.
'12345'.isdigit() # Output: True
isalnum(): Checks if all characters are alphanumeric (letters and numbers).
'Hello123'.isalnum() # Output: True
isspace(): Checks if all characters are whitespace.
' '.isspace() # Output: True
istitle(): Checks if the string is title-cased (first letter of each word is uppercase).
'Hello World'.istitle() # Output: True
7. String Alignment
center(width, fillchar=' '): Centers the string, padding with fillchar.
'Hello'.center(10, '-') # Output: '--Hello---'
ljust(width, fillchar=' '): Left-justifies the string.
'Hello'.ljust(10, '-') # Output: 'Hello-----'
rjust(width, fillchar=' '): Right-justifies the string.
'Hello'.rjust(10, '-') # Output: '-----Hello'
String Formatting
1. format() Method
You can insert values into a string using placeholders {}:
'My name is {}'.format('John') # Output: 'My name is John'
Positional and keyword arguments:
'Coordinates: {}, {}'.format(10, 20) # Output: 'Coordinates: 10, 20'
'Name: {first} {last}'.format(first='John', last='Doe') # Output: 'Name: John Doe'
2. Formatted String Literals (f-strings)
Introduced in Python 3.6, f-strings allow embedding expressions inside string literals using {}.
name = 'John'
age = 30
f'My name is {name} and I am {age} years old' # Output: 'My name is John and I am 30 years old'
3. % Operator (Old-style)
The % operator allows formatting similar to C's printf.
'Hello, %s' % 'World' # Output: 'Hello, World'
'Age: %d' % 30 # Output: 'Age: 30'
String Encoding and Decoding
encode(encoding='utf-8'): Encodes the string to bytes using the specified encoding.
'Hello'.encode('utf-8') # Output: b'Hello'
decode(encoding='utf-8'): Decodes bytes to a string.
b'Hello'.decode('utf-8') # Output: 'Hello'
String Slicing
Strings in Python can be sliced to extract substrings.
list[start:end] --> from index start to (end-1)
list[start:] ---> from index start to end of the list
list[:end] --> from index 0 to end-1
list[:] --> copy of whole array
Examples:
s = 'Hello World'
s[0:5] # Output: 'Hello' (characters from index 0 to 4)
s[:5] # Output: 'Hello' (same as above)
s[6:] # Output: 'World' (characters from index 6 to end)
s[-5:] # Output: 'World' (last 5 characters)
Common String Functions
max(): Returns the character with the maximum ASCII value.
max('Hello') # Output: 'o'
min(): Returns the character with the minimum ASCII value.
min('Hello') # Output: 'H'
ord(): Returns the ASCII value of a character.
ord('A') # Output: 65
chr(): Returns the character for an ASCII value.
chr(65) # Output: 'A'
This guide covers the majority of string methods and functions available in Python.