Using string ‘split’ method, we can convert a comma separated string to list.
Signature
str.split(sep=None, maxsplit=-1)
‘split’ method return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If maxsplit is not specified or -1, then there is no limit on the number of splits
Example
list_of_strs = str.split(',')
convert_string_to_list_of_tokens.py
str = "Ram,Rahim,Robert"
list_of_strs = str.split(',')
print('str -> ', str)
print('list_of_strs -> ', list_of_strs)
Output
str -> Ram,Rahim,Robert list_of_strs -> ['Ram', 'Rahim', 'Robert']
No comments:
Post a Comment