Repository navigation
Expand file tree
/
Copy pathstring_basics_exercise.py
More file actions
69 lines (55 loc) · 2.69 KB
/
Copy pathstring_basics_exercise.py
File metadata and controls
69 lines (55 loc) · 2.69 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
# Question 1: Given the string below, use the find method to determine
# what index the word "universe" starts in? (and print it)
my_string = "There's a lot going on in the universe don't you think?"
## Write your code below, 1 line ##
idx = my_string.find("universe")
## End question 1
# Question 2: Create a variable my_slice. Using a step size of 2, assign
# it to a slice of my_string from the beginning to the end (the whole string)
# including white spaces. Print how many characters are in this new string? (my_slice)
## Write your code below, 2 lines ##
my_slice = my_string[::2]
print(len(my_slice))
## End question 2
# Question 3: Given the two string below, use string concatenation to
# create and print one string (one followed by the other) seperated by a
# comma and a space ", "
bright_idea = "If we used yesterday's stock price instead"
result = "it would result in a 2% increase in the value of our assets"
## Write your code below, 1 line ##
arr = [bright_idea, result]
joiner = ", "
newStr = joiner.join(arr)
#print(newStr)
## End question 3
# Question 4: You can test whether one string is equal to another by using the
# equality test operator '=='. For example if you wanted to test whether "Harry"
# was equal to "Harry", you would use the following code "Harry" == "Harry".
# Python would evaluate that expression to True (capital T). If you tested if
# "Harry" == "harry" instead, Python would evaluate that to False (capital F)
# since "harry" in the second case is not capitalized. Given the string
# word_to_test below, write and print an equality test in one line to check if
# the string would be equal to a string if the order of its characters were
# reversed. Hint: You can use the slice reverse trick.
word_to_test = "level"
## Write your code below, 1 line ##
print("Is this equal?", word_to_test == word_to_test[-1::-1])
## End question 4
# Question 5: You can use the len function to get the total characters or
# 'length' of a string. For example len("hello") will give you 5. If you divide
# this length by 2 using the floor division operator '//', you can get the index
# at half of your string. You can then use slice notation to capture half of
# your string since you know the length//2. With this knowledge, print out the
# second half of the string below in one line. Output should be "I am the second
# half".
my_daily_thought = "I am the first half I am the second half"
## Write your code below, 1 line ##
start_index = len(my_daily_thought) // 2
print(my_daily_thought[start_index:])
## End question 5
# Question 6: Import the hexdigits constant from the string module and print
# them out.
## Write your code below, 2 lines ##
from string import hexdigits
print(hexdigits)
## End queston 6