I have a string for input with comma seperated elements. - marks = 'marks1,marks2,marks3'
Using split function I got list of marks. - marks = marks.split(',')
This gives us 'marks' a list with elements as srting however I want integers as elements. marks[int]
Do we have an in-built function to convert the same or do we need to use the following for-loop?
for i, m in enumerate(marks):
marks[i] = int(m)
The above loop works but I just want to know if we have some in-built function
If your string is 'marks1,marks2,marks3'
, you can't convert it directly to integers by casting, as it contains non-numeric characters.
But, if you used it to explain your problem and string is actually like '31,78,45'
, you can convert it to a list of int with list comprehension:
marks = [int(m) for m in marks]
marks = '31,78,45'
marks = marks.split(',')
marks = [int(m) for m in marks]