In this week, we continue to learn string, which is very important. Loop is also introduced. Examples like loop for a list, loop for a dictionary or loop for a string are taught.
We learned some useful functions of string.
- len(str) — find the length of present string
- str.find(“ab”) — search a string in present string
- str.rstrip() — remove whitespace
- str.replace(“red”,”green”) — replacement
- str.split(“,”) — split
- str.isdigit() — decide whether string is all digit
- lower(), upper() — change string to uppercase or lowercase
- str.endswith(“hello”) — test whether present string ends with another string
We talked about type conversion, like changing variable type from int to string or string to int.
Then we learned loop, which is about repeat steps/statements. We looked at the ‘while’ loop. ‘While’ loop can use an iteration variable to control the loop. There are generally two types of loop, finite loop and infinite loop. Finite loop stops when the termination condition is satisfied any more. Infinite loop never stops because the termination condition is never met. We learned ‘for’ loop then, which is very useful for iterating over a sequence. ‘for’ loop in a list is iterating over all elements in the list. ‘for’ loop in a dictionary is iterating over all keys of elements in the dictionary. ‘for’ loop in a string is iterating over all characters in a string.
With loop, we have the tool to scan data structures like list, dictionary and string without writing duplicate code.