Victor
Victor

Reputation: 65

Splitting a string in two parts

I want to split a string in two parts like this:

>>> label = ('A1')
['A', '1']

Is there any method to do this in python?

I tried:

>>> label = label.split(',')
['A1']

As you can see the comma is not printed.

Upvotes: 1

Views: 102

Answers (1)

user2555451
user2555451

Reputation:

You can simply use list:

>>> label = 'A1'
>>> list(label)
['A', '1']
>>>

list will iterate over the string and collect its characters into a new list.

Also, you cannot use str.split here because the method was designed to split on characters/substrings and remove them from the resulting list. For example, 'a b c'.split() would split on whitespace and remove those characters from the returned list, which is ['a', 'b', 'c']. You however want to break the string up into individual characters while still keeping all of them.

Upvotes: 4

Related Questions