Data Science - Sec2
Data Science - Sec2
Science
Section2
String Operations
and functions
String operations and functions (cont.)
➢ To get the length of a string, use the len()
function.
➢ find() Searches the string for a specified
value and returns the position of where it was
found.
✓Note : Both index() and find() are identical in that
they return the index position of the first occurrence
of the substring from the main string.
✓The main difference is that Python find() produces
-1 as output if it is unable to find the substring,
whereas index() throws a ValueError exception.
String operations and functions (cont.)
➢ replace() Returns a string where a
specified value is replaced with a
specified value.
✓Note: this function not modified the original
variable.
➢ count() Returns the number of times a
specified value occurs in a string.
➢ F-strings provide a concise and
convenient way to embed python
expressions inside string literals for
formatting.
While loop
iterations
➢Iteration means executing the same block of code over and over,
potentially many times. A programming structure that implements
iteration is called a loop.
➢Example of basic while loop syntax:
while <expr>:
<statement(s)>