-1

This is the string I am given Enter 3 random strings, separated by commas: The,Dave,Make

I want the list to only include this: ["The", "Dave", "Make"]

I have tried using split, but it result in an result an error

strings = input("Enter 3 random strings, separated by commas:")
strings = strings.split()
Lim Han Yang
  • 350
  • 6
  • 23

4 Answers4

1

Use string.split() with strip() methods:

In [2680]: s = "Enter 3 random strings, separated by commas: The,Dave,Make"
In [2686]: s.split(':')[-1].strip().split(',')
Out[2686]: ['The', 'Dave', 'Make']
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Try this:

string = 'Enter 3 random strings, separated by commas: The,Dave,Make'

lst = string.split(':')[-1].strip().split(',')

Output:

>>> lst
['The', 'Dave', 'Make']
Sushil
  • 5,440
  • 1
  • 8
  • 26
1

Split the string at the colon, remove extra whitespace, then split at commas.

string = 'Enter 3 random strings, separated by commas: The,Dave,Make'
result = string.split(':')[1].strip().split(',')
ThatCoolCoder
  • 249
  • 3
  • 16
1

If the string is obtained from the console, its first part should be the input prompt:

strings = input("Enter 3 random strings, separated by commas:")
strings = strings.strip().split(',')

The code can be shortened into a one-liner:

strings = input("Enter 3 random strings, separated by commas:").strip().split(',')
GZ0
  • 4,055
  • 1
  • 10
  • 21