-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
  • 117
  • 2
  • 14

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
  • 31,737
  • 7
  • 30
  • 50
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,231
  • 1
  • 7
  • 23
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
  • 219
  • 4
  • 11
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
  • 3,852
  • 1
  • 6
  • 20