Data Types - String Flashcards

(46 cards)

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, returns True

A

Return “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

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

Remember to store it in a variable since strings are immutable

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)

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

Print “Pyt” from “Python”

A

print(“Python”[ :3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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: ])

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 wants to return 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
Give two methods to 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 each individual character**
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"
43
def solution(string, ending): Complete the function above so that it returns true if the first argument "string" ends with the 2nd argument "ending". E.g., string, ending = "abc", "bc" # True string, ending = "abc", "d" # False
def solution(string, ending): return string.endswith(ending)
44
What is the .endswith() method, and how do you use it?
.endswith() checks if a string ends with the given suffix. It returns True if it does, otherwise False. E.g., "photo.jpeg".endswith(".jpg") # True
45
How do you check if a string ends with any of multiple suffixes?
Pass a tuple of suffixes to .endswith() E.g., filename = "photo.jpeg" filename.endswith((".png", ".jpg", ".jpeg")) # True
46
text = "python_programming" Using .endswith(), check if "python" in "text" ends with "hon"
text.endswith("hon", 0, 6) # True ## Footnote Syntax: string.endswith(suffix, start, end)