forked from bradtraversy/python_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.py
More file actions
62 lines (41 loc) · 997 Bytes
/
strings.py
File metadata and controls
62 lines (41 loc) · 997 Bytes
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
# Strings in python are surrounded by either single or double quotation marks. Let's look at string formatting and some string methods
name = 'Brad'
age = 37
# Concatenate
print('Hello, my name is ' + name + ' and I am ' + str(age))
# String Formatting
# Arguments by position
print('My name is {name} and I am {age}'.format(name=name, age=age))
# F-Strings (3.6+)
print(f'Hello, my name is {name} and I am {age}')
# String Methods
s = 'helloworld'
# Capitalize string
print(s.capitalize())
# Make all uppercase
print(s.upper())
# Make all lower
print(s.lower())
# Swap case
print(s.swapcase())
# Get length
print(len(s))
# Replace
print(s.replace('world', 'everyone'))
# Count
sub = 'h'
print(s.count(sub))
# Starts with
print(s.startswith('hello'))
# Ends with
print(s.endswith('d'))
# Split into a list
print(s.split())
# Find position
print(s.find('r'))
# Is all alphanumeric
print(s.isalnum())
# Is all alphabetic
print(s.isalpha())
# Is all numeric
print(s.isnumeric())