-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_methods.py
More file actions
64 lines (44 loc) · 1.42 KB
/
str_methods.py
File metadata and controls
64 lines (44 loc) · 1.42 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
### SPLIT ###
# splits the string based on specified separator and returns a list
# default separator is white space
# str.split("[SEPARATOR]")
shopping = "clothes:shoes:toys:makeup:jewelery"
answr1 = shopping.split(":")
print(answr1)
### JOIN ###
# joins all elements in an iterable by using the specified separator and returns a string
# Separator can be a char, space, no space, symbol, num
# ([SEPARATOR]).join(iterable)
n1 = "123" # Iterable
n2 ="aaa" # Separator
answr2 = n2.join(n1)
print(answr2)
### UPPER ###
# changes all chars in a str to uppercase
sen = "dogs are very cute"
answr3 = sen.upper()
print(answr3)
### LOWER ###
# changes all char in str to lowercase
line = "I LIKE TO PLAY BASKETBALL"
answr4 = line.lower()
print(answr4)
### TITLE ##
# Capitalizes all words in a str
name = "mickey mouse"
answr5 = name.title()
print(answr5)
### REPLACE ###
# replaces a specified str with a different str
# 3 main parameters
# str to replace, replacement str, amount of replacements (optional)
# note: if 3rd param is not called, it will replace all occurences
# str.replace("old str", "new str", "occurences")
poem = "ow ow ow your boat"
answr6 = poem.replace("ow", "row")
print(answr6)
### STRIP ##
# removes the specified space(s)/value/char at the start and end of a str
snack = "fruitsalad tastes good fruit"
answr7 = snack.strip("fruit")
print(answr7)