Python Composite data types Flashcards
myStr = “Hello World”
print(myStr[2:-4:2])
‘loW’
H is index 0
index -4 is o but is not included
step size is 2
no comma!
str1 = “ABC”
str2 = “a b”
newStr = str1 * len(str2)
print(newStr)
ABCABCABC
Space and Tabs are also entities within the string, hence it is counted as len
str1 = "ababc" str2 = "ab" if str2 * len(str2) in str1: print("case1") elif str2 in str1: print("case2") else: print("case3")
Result = case1
str2len(str2) = ab2
ab*2 is in ababc
thus case1
membership operation, whats the operator used
in
Are string immutable?
yes
aStr = 'spam' newStr = aStr[:1] + 'l' + aStr[2:]
result = slam
str1 = “couple”
str2 = “tea”
newStr = str2[:1] + str1[2:]
print(newStr, str1)
tuple couple
what does string.find do
find the character within the string and returns its index value.
if the character does not exist, return -1
str1 = “couple”
newStr = str1[str1.find(‘ou’):str1.find(‘l’)]
print(newStr)
result= oup
str1 = “couple“
str2 = “t”
newStr = str1[::str1.find(str2)]
print(newStr)
elpuoc
string step size =-1 as str2 cannot be found in str1 hence -1.
with a step size of minus 1, the string gets reversed
string.upper
converts all the string to upper cases
string.lower
converts all the strings to lower cases
string.find
find the first character within the string and returns the index position of it.
if not found, return the value of 0
string.capitalize
capitalizes the first part of the string only, subsequent strings are not included.
string.index
similar to string.find but returns exception error when not found