-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyTutorial-Strings.py
86 lines (59 loc) · 2.45 KB
/
PyTutorial-Strings.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
# https://www.youtube.com/watch?v=k9TUPpGqYTo&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&index=2
message = 'hello world'
# Text can be enclosed in ' or " depending on what is in the string
message_test = '''Multiple
Lines
Of Text'''
# If the string spans multiples lines it should be enclosed in ''' or """
print(message)
print(message_test)
print(len(message))
print(message[0])
print(message[10])
print(message[0:5])
# first index is inclusive, second index is NOT
print(message[:5])
# if you leave out the first index Python will assume the beginning of the string
print(message[6:])
# if you leave off the last index Python will assume the end of the string
#String methods----------------------------
print(message.lower())
# change string to all lowercase--------------
print(message.upper())
# change string to all uppercase--------------
print(message.count('l'))
# count the number of times the selected character(s) occur in a string
#---------------------------------------------
print(message.find('l'))
# returns the index of the first instance of the character(s) in the string. If there is no instance it will return -1
#---------------------------------------------
message2 = 'Hello World'
print(message2)
message3 = message2.replace('World', 'Universe')
print(message3)
# assigns the replaced string to a new variable. The original does not change.
message2 = message2.replace('World', 'Universe')
print(message2)
# assigns the replaced string to the original variable. The original variable is now changed.
#-------------------------------
greeting = 'Hello'
name = 'Michael'
full_greeting = greeting + name
print(full_greeting)
#prints both variables with no space between
full_greeting = greeting + ', ' + name
print(full_greeting)
#prints variables with some formatting
full_greeting = '{}, {}. Welcome!'.format(greeting, name)
print(full_greeting)
#prints variables using a formatted string. {} is replaced with the variable names in order.
full_greeting = f'{greeting}, {name}. Welcome!'
print(full_greeting)
# new fstring. works in 3.6 and up. variables can call methods if desired (ie {name.upper})
#---------------------------------------------
print(dir(name))
#displays a list of methods available for a variable
#-------------------------------------------------------
print(help(str))
#displays the help file for a method
#-----------------------------------------