Repository navigation
Expand file tree
/
Copy path003_strings.py
More file actions
216 lines (166 loc) · 6.96 KB
/
Copy path003_strings.py
File metadata and controls
216 lines (166 loc) · 6.96 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
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
## >---
## >YamlDesc: CONTENT-ARTICLE
## >Title: python strings
## >MetaDescription: python string, Strings as arrays,Slicing Strings,String Methods,find,index example code, tutorials
## >MetaKeywords: python string, Strings as arrays,Slicing Strings,String Methods,find,index example code, tutorials
## >Author: Venkata Bhattaram / tinitiate.com
## >ContentName: strings
## >---
## ># Python String handling
## >* In python Strings are sequence of characters in single or double quotes
## >* Multi-line strings are enclosed in Triple quotes """<Multi Line text>"""
## > or '''<Multi Line text>'''
## >* Working with String character index(position of a character in a string)
## >* Strings are immutable, meaning they cant be changed once assogned
## > * Slicing strings with INDEX
## > * String built-in methods
## > * String text Case conversion
## > * String Checking methods
## > * String Manipulation
## >### Strings Basics
## >```
# sample string using DOUBLE QUOTES
var_string_1 = "DOUBLE QUOTES STRING:Welcome to python tutorials by tinitiate.com"
# sample string using SINGLE QUOTES
var_string_2 = 'SINGLE QUOTES STRING: Welcome to python tutorials by tinitiate.com'
# Multi line string using THREE DOUBLE QUOTES
var_string_3 = """THREE DOUBLE QUOTES MULTI LINE STRING: Welcome to python
tutorials by tinitiate.com,This is a multi line string
as you can see"""
# print the sthe above strings
print(var_string_1)
print(var_string_2)
print(var_string_3)
## >```
## >### Strings as arrays [Using strings as arrays in other languages]
## >```
# Using python strings as STRING ARRAYs
var_test_string = "Python is cool"
# Index starts from 0,
# This prints the first character of the string
print('First character of variable var_test_string: ', var_test_string[0]);
# Index of the last character is -1
print('Last character of variable var_test_string: ',var_test_string[-1]);
# Print forth character from the end
print('Fourth character from the end of variable var_test_string: '
,var_test_string[-4]);
## >```
## >### Slicing Strings[ working with string indexes(character positions in the string)]
## >```
# Slicing part of the string using [number:]
# prints string from specified index position to end of string
print(var_test_string[6:])
# prints string from specified index position to end of string
print(var_test_string[-4:])
# prints a part of the string between the specified index position
print(var_test_string[4:10])
# OUT oF range indexes
# when specifing indexes that dont exist is a string
# single index usage fails
var_my_string = "four"
# print(var_my_string[5]) # this is raise an error
# Slicing will not raise error, rather will not print anything
print(var_my_string[5:7]) # Doesnt print anything
## >```
## >### String Methods - Text Case conversion
## >```
# Testing String
var_string_case_test = "learn PYTHON"
# upper case conversion
print(var_string_case_test.upper()) # OUTPUT: LEARN PYTHON
# lower case conversion
print(var_string_case_test.lower()) # OUTPUT: learn python
# title case conversion
print(var_string_case_test.title()) # OUTPUT: Learn Python
# swap case conversion, swaps upper to lower and vice versa
print(var_string_case_test.swapcase()) # OUTPUT: LEARN python
# capitalize case conversion, UPPERs the first letter of the string
print(var_string_case_test.capitalize()) # OUTPUT: Learn python
## >```
## >### String Methods - Checking methods
## >```
# The Checking methods print true or false (Boolean)
# Checking if the string is UpperCase
print ('PYTHON'.isupper()) # prints true
# Checking if the string is lowerCase
print ('PYTHON'.islower()) # prints false
# Checking if the string is alphabets only
print ('PYTHON01'.islower()) # prints false
# Checking if the string is alphnumeric (alphabets/digits)
print ('PYTHON01'.isalnum()) # prints true
# Checking if the string is alphnumeric (alphabets/digits/special characters)
print ('1000'.isnumeric()) # prints true
# Checking if the string is white space (space/tab)
print (' '.isspace()) # prints true
## >```
## >### String Methods - String Manipulation
## >```
# remove parts of string
# and return the changed string
# strip() Removes the white spaces before and after the string
print (' This is a test '.strip())
# OUTPUT: This is a test
print ('This is a test'.strip('t')) # removes the last occurrence of 't'
# OUTPUT: This is a tes
print ('This is a test'.strip('T')) # removes the last occurrence of 'T'
# OUTPUT: his is a tes
print ('This is a test'.lstrip('This')) # removes the last occurrence of 'This'
# OUTPUT: is a test
# lstrip() Removes the leading characters or white spaces by default
# white spaces, of a string
# Removes the white spaces before and after the string
print (' This is a test '.lstrip())
# OUTPUT: This is a test
print ('This is a test '.lstrip('This'))
# OUTPUT: is a test
# rstrip() Removes the trailing characters or white spaces by default
# white spaces, of a string
# Removes the white spaces before and after the string
print ('This is a test '.rstrip(' test'))
# OUTPUT: This is a
## >```
## >### String alignment justify
## >```
# ljust, center, rjust
# justify methods that add white spaces by default to
# align the string, and pad the string to the length mentioned
print ('test'.ljust(10,'+'))
# OUTPUT: test++++++
print ('test'.rjust(10,'+'))
# OUTPUT: ++++++test
print ('test'.center(10,'+'))
# OUTPUT: +++test+++
## >```
## >### Work with portions of string
## >```
# find()/index(): displays the position index of a given substring and
# its occurrence number
print ('test'.find('t',2)) # print the index of the second occurrence of 't'
# OUTPUT: 3
print ('test'.index('t')) # print the index of the first occurrence of 't'
# OUTPUT: 0
# find() doesnot raise error, it returns -1 when not found
print ('test'.find('z')) # print the index of the second occurrence of 't'
# OUTPUT: -1
# index(), raises an error when not found
# print ('test'.index('z')) # print the index of the second occurrence of 't'
# ERROR: ValueError: substring not found
#rfind(), prints the highest index and -1 when not found
print ('test'.rfind('z')) # OUTPUT: -1
print ('test'.rfind('t')) # OUTPUT: 3
#rindex(), prints the highest index and errors out when not found
print ('test'.rindex('t')) # OUTPUT: 3
#replace(); replace the characters with the specified characters
print ('test'.replace('t','i')) # OUTPUT: iesi
#split(), This converts a string into a list based on a separator,
#default is whitespace
print('This is a test'.split()) # prints a LIST, seperated by a whitespace
print('This|is|a|test'.split('|')) # prints a LIST, seperated by a pipe |
#splitlines(), Converts multi-line strings to a list
# Test varaible with multi-line string
testString = """Line 1
Line 2
Line 3
"""
print(testString.splitlines()) # OUTPUT: ['Line 1', 'Line 2', 'Line 3']
## >```