0

For example if I have

x = "12345;9876"
y = ?
z = ?

how can i make it so y is only the part before the semicolon and z is the part after the semicolon

Benomat
  • 11
  • 2

4 Answers4

2
y, z = x.split(';')

This uses two features:

  • The split function (method) to split the string into two parts (or more, if there are more semicolons)
  • Tuple unpacking, which lets us assign a list (or similar) to several variables, putting each item of the list into the corresponding variable
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
1

Fairly simple:

y, z = x.split(";")
user2390182
  • 72,016
  • 6
  • 67
  • 89
1
x = "12345;9876"
y, z=x.split(';')
1

You can try this -

x = "12345;9876"

y = x.split(';')[0]

z = x.split(';')[1]

print(y)
print(z)

Result:

12345 # This is y
9876 # This is z
PCM
  • 2,881
  • 2
  • 8
  • 30