Data Types - String Flashcards
An easy method to change a string to all capitals
“Hello”.upper()
(= “HELLO”)
An easy method to change a string to all lower case
“Hello”.lower()
(= “hello”)
An easy method to capitalize the first character of each word in a string
“hello world”.title()
(= “Hello World”)
Join the below 2 strings into a sentence using String Concatenation,
“Hello”
“World”
“Hello” + “ “ + “World”
Join the below 2 strings into a sentence using f-strings,
“Hello”
name = “Richard”
f”Hello {name}”
Give 2 efficient methods of finding the starting index number of the sub-string “love” in “I love Python”
“I love Python”.index(“love”)
&
“I love Python”.find(“love”)
For finding the starting index number of a sub-string in a string, what is the difference between the following 2 methods,
.find()
.index()
.index() - returns an error
&
.find() - returns -1
A method to find how many times “a” appears in “apples and pears”
“apples and pears”.count(“a”)
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
“love” in “I love Python”
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)
“love” not in “I love Python”
or
not “love” in “I love Python”
A method to replace “hate” with “love” in the string below”
“I hate Python”
“I hate python”.replace(“hate”, “love”)
A method that changes;
“apple,banana,orange”
to
[“apple”, “banana”, “orange”]
“apple,banana,orange”.split(“,”)
A method that changes;
[“2024”, “12”, “31”]
to
“2024-12-31”
”-“.join([“2024”, “12”, “31”])
A method that changes;
“ABC”
to
“A/B/C”
”/”.join(“ABC”)
A method that changes
“ hello world ! “
to
“hello world !”
” hello world ! “.strip()
(removes whitespace from the beginning and the end of a string - but not from the middle)
A method that changes
“ hello world ! “
to
“hello world ! “
” hello world ! “.lstrip()
(removes whitespace from the left of a string - but not from the right or the middle)
A method that changes
“ hello world ! “
to
“ hello world !”
” hello world ! “.rstrip()
(removes whitespace from the right of a string - but not from the left or the middle)
Print “Pyt” from “Python”
print(“Python”[ :3])
Print “tho” from “Python”
print(“Python”[2:5])
Print “hon” from “Python”
Give two methods of slicing
1)
print(“Python”[3: ])
2)
print(“Python”[-3: ])
In solution 1, the “:” is very important! Without it, the result would be “h” !!
Print “Pto” from “Python”
print(“Python”[ : :2])
Print “yhn” from “Python”
print(“Python”[1: :2])
Print “nhy” from “Python”
print(“Python”[ : :-2])
or
print(“Python”[-1: :-2])
Print “otP” from “Python”
print(“Python”[-2: :-2])