Data Types - String Flashcards

1
Q

An easy method to change a string to all capitals

A

“Hello”.upper()

(= “HELLO”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An easy method to change a string to all lower case

A

“Hello”.lower()

(= “hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

An easy method to capitalize the first character of each word in a string

A

“hello world”.title()

(= “Hello World”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Join the below 2 strings into a sentence using String Concatenation,

“Hello”
“World”

A

“Hello” + “ “ + “World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Join the below 2 strings into a sentence using f-strings,

“Hello”
name = “Richard”

A

f”Hello {name}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give 2 efficient methods of finding the starting index number of the sub-string “love” in “I love Python”

A

“I love Python”.index(“love”)
&
“I love Python”.find(“love”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

For finding the starting index number of a sub-string in a string, what is the difference between the following 2 methods,

.find()
.index()

A

.index() - returns an error
&
.find() - returns -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A method to find how many times “a” appears in “apples and pears”

A

“apples and pears”.count(“a”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a single-line truthy/falsy script that will check if the sub-string “love” is within the string “I love Python”, and if so, evaluate as True

A

“love” in “I love Python”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a single-line truthy/falsy script that will check if the sub-string “love” is within the string “I love Python”, and if so, evaluate as False

(It should merely evaluate as False, not return False)

A

“love” not in “I love Python”
or
not “love” in “I love Python”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A method to replace “hate” with “love” in the string below”

“I hate Python”

A

“I hate python”.replace(“hate”, “love”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A method that changes;
“apple,banana,orange”
to
[“apple”, “banana”, “orange”]

A

“apple,banana,orange”.split(“,”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A method that changes;
[“2024”, “12”, “31”]
to
“2024-12-31”

A

”-“.join([“2024”, “12”, “31”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A method that changes;
“ABC”
to
“A/B/C”

A

”/”.join(“ABC”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A method that changes
“ hello world ! “
to
“hello world !”

A

” hello world ! “.strip()

(removes whitespace from the beginning and the end of a string - but not from the middle)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A method that changes
“ hello world ! “
to
“hello world ! “

A

” hello world ! “.lstrip()

(removes whitespace from the left of a string - but not from the right or the middle)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

A method that changes
“ hello world ! “
to
“ hello world !”

A

” hello world ! “.rstrip()

(removes whitespace from the right of a string - but not from the left or the middle)

18
Q

Print “Pyt” from “Python”

A

print(“Python”[ :3])

19
Q

Print “tho” from “Python”

A

print(“Python”[2:5])

20
Q

Print “hon” from “Python”

Give two methods of slicing

A

1)
print(“Python”[3: ])

2)
print(“Python”[-3: ])

In solution 1, the “:” is very important! Without it, the result would be “h” !!

21
Q

Print “Pto” from “Python”

A

print(“Python”[ : :2])

22
Q

Print “yhn” from “Python”

A

print(“Python”[1: :2])

23
Q

Print “nhy” from “Python”

A

print(“Python”[ : :-2])

or

print(“Python”[-1: :-2])

24
Q

Print “otP” from “Python”

A

print(“Python”[-2: :-2])

25
What's the difference between the below, print("Python"[ : :-2]) vs print("Python"[-1: :-2])
Nothing - they give the same result
26
Print "n" from "Python"
print("Python"[-1])
27
print("*" * 3) What will the terminal output be
Terminal output = ***
28
a = '1 2 3' a.split() print(a) What will the above print and why
It will print, 1 2 3 (I.e., unaltered), rather than, ['1', '2', '3'] .split() always returns a NEW list — it does not modify the original string "in place". To use the result, it should be assigned to a variable like this: a = a.split() ## Footnote 1) .split() is a method that only operates on strings. Remember, Strings are immutable, so they can never be modified "in place".
29
var = "abc".split("") What will the above return Why
ValueError Although .split() requires a string as a separator, it must be a non-empty string that says where to split the string into a list
30
Turn "abc" into ["a", "b", "c"]
list("abc")
31
Turn "16 1 246 7" into ["16", "1", "246", "7"]
var = "16 1 246 7".split( ) ## Footnote An empty .split() splits by whitespace - same as .split(" ") Note that, since a string is immutable, the result should be stored in a variable to work
32
Turn "a,b,c" into ["a", "b", "c"]
var = "a,b,c".split(",") | An empty .split() splits by whitespace - same as .split(" ") ## Footnote Note that, since a string is immutable, the result should be stored in a variable to work
33
Change, "hELLO wORLD" To, "Hello World"
word = "hELLO wORLD".swapcase()
34
for char in txt: print(txt[char]) What does the above print
TypeError This treats "char" like it's an index, but it actually refers to an item (in this case a character in "txt"). If you want to print the Item/character, it should be typed as below, for char in txt: print(char)
35
How can you check if a character is uppercase or lowercase
.isupper() .islower() | These will return True or False
36
Print "P" from "Python"
print("Python"[0]) ## Footnote print("Python"[ ]) returns SyntaxError as there is no default index number when trying to slice with empty square brackets
37
"short sentence".count("a","e","i","o","u") What will the above return if printed
TypeError the ".count()" method can only take one parameter at a time
38
def get_count(sentence): total_count = 0 total_count += sentence.count("a") total_count += sentence.count("e") total_count += sentence.count("i") total_count += sentence.count("o") total_count += sentence.count("u") return total_count Give a shorter way of writing the above
def get_count(sentence): total_count = 0 for char in "aeiou": total_count += sentence.count(char) return total_count
39
"Hello World !" Loop through this String checking if **each** character is alphabetical & if so, print True
for char in "Hello World !": if char.isalpha(): print(True)
40
num = int(2) Print the above int as the String "002" by using **f-strings** and **padding**
num = int(2) print(f"{num:03}") ## Footnote In ":03" the "3" is the total number of digits required, and "0" is the required leading numbers
41
.title() vs .capitalize()
.capitalize() capitalizes the first character in a String .title() capitalizes the first character of **every** word in a String
42
nums = "part one" "part two" "part three" What does print(nums) print
An error To avoid this error, you must use "\" as follows; nums = "part one"\ "part two"\ "part three" this prints "part onepart twopart three"