Wednesday, July 20, 2022

[FIXED] How to split a string into a string and an integer?

Issue

How do you split a string into a several strings in python. The string I am trying to split is in the format of:

Variable 1

and I would like to split it into:

Variable = Variable

Number = 1


Solution

You could split based on the space in the example you gave, unless you need a more generic approach.


# set up your variable
my_var = "variable 1"

# You can split by the space in this instance, if all of your data will look the same
my_split = my_var.split(" ")

# prints variable
print(my_split[0])

# prints 1
print(my_split[1])



Answered By - syntheticgio
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.