224k views
3 votes
The function below takes two parameters: a string parameter: csv_string and an integer index. This string parameter will hold a comma-separated collection of integers: ‘111, 22,3333,4’. Complete the function to return the indexth value (counting from zero) from the comma-separated values as an integer. For example, your code should return 3333 (not ‘3333 *) if the example string is provided with an index value of 2. Hints you should consider using the split() method and the int() function. print.py 1 – def get_nth_int_from_CSV(CSV_string, index): Show transcribed image text The function below takes two parameters: a string parameter: csv_string and an integer index. This string parameter will hold a comma-separated collection of integers: ‘111, 22,3333,4’. Complete the function to return the indexth value (counting from zero) from the comma-separated values as an integer. For example, your code should return 3333 (not ‘3333 *) if the example string is provided with an index value of 2. Hints you should consider using the split() method and the int() function. print.py 1 – def get_nth_int_from_CSV(CSV_string, index):

User Jekayode
by
3.7k points

1 Answer

6 votes

Answer:

The complete function is as follows:

def get_nth_int_from_CSV(CSV_string, index):

splitstring = CSV_string.split(",")

print(int(splitstring[index]))

Step-by-step explanation:

This defines the function

def get_nth_int_from_CSV(CSV_string, index):

This splits the string by comma (,)

splitstring = CSV_string.split(",")

This gets the string at the index position, converts it to an integer and print the converted integer

print(int(splitstring[index]))

User Loc Phan
by
4.1k points