2017-02-15 HW

posted Feb 15, 2017, 6:39 AM by Samuel Konstantinovich   [ updated Feb 15, 2017, 7:33 AM ]
Some more string methods:

We can replace parts of a string, and make a new string, but the original doesn't change
s = "abcd"
print s.replace('bc','XYZ') # prints 'aXYZd'
print s # prints 'abcd'



We can see if a string ends with a different string.
s.endswith('ca') # False
s.endswith('cd') # True


We can check if strings are letters, numbers, or both!
"asdf".isalpha() # evaluates to True 
"asdf2".isalpha() # evaluates to False 

'123'.isdigit() # True 
'123.45'.isdigit() # False 

"asdf2".isalnum() # evaluates to True 

We can remove spaces, or characters we don't want from the left/right sides of a string!
" ab ".strip() # 'ab'  
"abcddda".strip("ad") # 'bc' 




CLASSWORK + Complete as homework:
Complete Codingbat String-2 and the new custom problems: 

Note: 
    -The last problem of String-2 is challenging! 
    -You may use all built-in methods to solve String-2
    -The custom problems restrict you from using a method, pay attention
Comments