-1

how to use split to separete 'g5s,12-04-2019' from comma.

I search for result if a='g5s,12-04-2019' then after split b = 'g5s' and c='12-04-1=2019'

tituszban
  • 4,797
  • 2
  • 19
  • 30
rupali
  • 3
  • 4
  • 4
    Possible duplicate of [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – Fabian Sep 23 '19 at 11:56

2 Answers2

1

str.split() to the rescue:

a='g5s,12-04-2019'
b, c = a.split(",")
tituszban
  • 4,797
  • 2
  • 19
  • 30
0

You should use the x.split() and pass the comma as input argument, like this x.split(","). It means the input argument is the separator.

The split method returns a list which contain the elements separately. You you can unpack them one by one. Like this: b, c = a.split(",")

Code:

a = "g5s,12-04-2019"
print("a={a}".format(a=a))
b, c = a.split(",")
print("b={b}, c={c}".format(b=b, c=c))

Output:

>>> python3 test.py 
a=g5s,12-04-2019
b=g5s, c=12-04-2019

FYI:

The docstring of the split method.

S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.
milanbalazs
  • 4,811
  • 4
  • 23
  • 45