# University of california all these string methods are

• Lab Report
• rf15707
• 36
• 88% (8) 7 out of 8 people found this document helpful

This preview shows page 7 - 10 out of 36 pages.

'University Of California' All these string methods are useful, but most programmers don't memorize their names or how to use them. Instead, people usually just search the internet for documentation and examples. A complete list of string methods appears in the Python language documentation. Stack Overflow has a huge database of answered questions that often demonstrate how to use these methods to achieve various ends. 2.2. Converting to and from Strings Strings and numbers are different types of values, even when a string contains the digits of a number. For example, evaluating the following cell causes an error because an integer cannot be added to a string. In [21]: 8 + "8" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-21-ea74adbd7634> in <module> () ----> 1 8 + "8" TypeError : unsupported operand type(s) for +: 'int' and 'str' However, there are built-in functions to convert numbers to strings and strings to numbers. Function name Effect Example int Converts a string of digits and perhaps a negative sign to an integer ( int ) value int("42") float Converts a string of digits and perhaps a negative sign and decimal point to a decimal ( float ) value float("4.2" ) str Converts any value to a string ( str ) value str(42) Try to predict what the following cell will evaluate to, then evaluate it. In [22]: 8 + int("8") Out[22]: 16 Suppose you're writing a program that looks for dates in a text, and you want your program to find the amount of time that elapsed between two years it has identified. It doesn't make sense to subtract two texts, but you can first convert the text containing the years into numbers. Question 2.2.1. Finish the code below to compute the number of years that elapsed between one_year and another_year . Don't just write the numbers 1618 and 1648 (or 30 ); use a conversion function to turn the given text data into numbers.
In [65]: # Some text data: one_year = "1618" another_year = "1648" # Complete the next line. Note that we can't just write: # another_year - one_year # If you don't see why, try seeing what happens when you # write that here. difference = int(another_year)-int(one_year) difference Out[65]: 30 In [66]: _ = ok.grade('q221') ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 2.2.2. Use replace and int together to compute the difference between the the year 753 BC ( the founding of Rome ) and the year 410 AD ( the sack of Rome ). Try not to use any numbers in your solution, but instead manipulate the strings that are provided.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 3 Failed: 0 [ooooooooook] 100.0% passed 2.3. Strings as function arguments String values, like numbers, can be arguments to functions and can be returned by functions. The function len