-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_strings_test.py
104 lines (93 loc) · 3.31 KB
/
03_strings_test.py
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
# Demonstrates various python string methods. For f strings, see
# 13_input_output_test.py
texts = []
# Demonstrates \used to escape special characters as well as addition and
# multiplication.
texts = texts + ['spam said ' + 2*'\'eggs\'']
# Demonstrates r used to indicate rawtext as well as quoted string autojoin.
# r is often used with regex.
texts.append(
r'^spam\s[a-z]{3}'
r'\seggs?'
)
# Demonstrates multiline string without \n. \ is needed to escape autonewline.
texts.append('''\
spam
and
eggs\
''')
print('appended text:')
for t in texts:
print(t)
print()
# Demonstrates use of the % operator to insert a %d int, %s string,
# and %.*f a float with * decimal places.
temperature_today = 'The temperature at %dpm in %s today was %.*f degrees Celcius.' % (
5, 'Singapore', 2, 30.4267)
print('using the % operator:')
print(temperature_today)
print()
# Demonstrates different ways to slice a string.
# 5 is the 6th char from the front. -7 is the 7th char from the back.
# slice(start, end, step=1) allows you to slice multiple chars at once. To
# avoid specifying the end, None can be used instead of len(string).
slice_text = 'spam and eggs'
# The default string slice can have 3 parameters:
# string_slice[start:stop:step]
slices = [5, -7, len(slice_text) - 6, slice(0, 6)]
print('string slicing:')
for s in slices:
print(slice_text[s])
print()
# Demonstrates list method insert.
insert_text = 'spam and eggs'
# str.split(seperator, max) split a string into a list.
# By default, it splits along all whitespace word boundaries.
insert_text_list = insert_text.split(' ')
for i in insert_text_list:
if 'spam' in i:
# If the string inserted ahead of the searched string contains the
# searched string, it results in an infinite loop.
# That's why you generally just create a new list instead of
# Trying to modify existing ones.
insert_text_list.insert(insert_text_list.index(i) + 1, 'ham')
# Prints list without commas.
print('insert string:')
print(*insert_text_list, '\n', sep=' ')
# Demonstrates common functions used on strings.
text = '_spam spam and eggs_'
# Case manipulation.
print('lower/upper/title casing:')
print(text.lower())
print(text.upper())
print(text.title())
print()
# Removes _ from front and back.
print('strip:', text.strip('_'))
# Tests if all of string is of type.
# Also demonstrates use of more than 1 function at a time.
# isupper and islower are also functions.
print('tests:', text.strip('_').isalpha(), text.isdigit(), text.isspace())
# Tests if string starts or ends with something.
print('start or end with:', text.startswith('spam'), text.endswith('_'))
# Replace all instances of a string inside a string with another.
print('replace:', text.replace('spam', 'eggs'))
# Opposite of split.
print('join:', ' '.join(insert_text_list))
# Returns index of first instance of substring in string.
# Returns -1 if substring is not found.
print('find:', text.find('eggs'))
# It is possible to use index() instead of find.
# index returns ValueError if substring is not found.
# index() can be applied to lists as well.
print('index:', text.index('eggs'))
# python supports special escape (\) characters as defined below:
# \n new line
# \r carriage return
# \f form feed
# \b backspace
# \t tab
# \' single quote
# \\ backslash
# \ooo Octal character (ooo)
# \xhh Hex value (hh)