-1

Say for example you have a variable with a string which contains x = '2 C', meaning 2 of clubs.

Is there a way of extracting the string (c) and storing it in a variable, i.e. string_var.

Then extract the number (2) into a different variable, i.e. number_var.

2
  • Is that structure permanent? Can there NOT be a space between them? Can they be in the reversed order? And most importantly, what have you tried to solve your problem? Commented May 18, 2020 at 18:25
  • I have tried to find the number in the string then I tried to remove from the string using var.remove() method but that didn’t work. My questions has now been answered Commented May 18, 2020 at 18:28

1 Answer 1

1

That's possible, you just need to spilt the string:

x = '2 C'
y = x.split() # returns ["2", "C"]
number = y[0] # do int(y[0]) if you want it to be an int
card_type = y[1]

All that did was split the variable x by a space, then store the output in variables.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.