-2

Hey I want to code a extractor :

for example

varall = "hello:my:whats:up"

then I want 4 differents ouput for example:

var1 = hello

var2 = my

var3 = whats

var4 = up

So I want from varall convert these 4 words into 4 differnte variables

Thanks!

Sid
  • 2,174
  • 1
  • 13
  • 29
maxowi
  • 1

3 Answers3

0

Use the split() method:

varall = "hello:my:whats:up"
varlst = varall.split(':') #splits string at the character ':'
for var in varlst:
    print(var)
Sid
  • 2,174
  • 1
  • 13
  • 29
  • yes Thanks thats acutally very good, how can I get it into 4 different varibales ? – maxowi Apr 17 '21 at 16:02
  • If you already know the no. of variables that will be in the string, you could use sth. like: `var1 = varlst[0]; var2 = varlst[1]`... and so on. OR you could use a dictionary. – Sid Apr 17 '21 at 16:04
  • @maxowi if it helped then please accept the answer. – Sid Apr 18 '21 at 04:43
0

Use split function to achieve that.

txt = "hello:my:whats:up"
x = txt.split(":")
print(x)
   
#Output - ['hello', 'my', 'whats', 'up']
Ashish M J
  • 642
  • 1
  • 5
  • 11
0
varall = "hello:my:whats:up"
var1,var2,var3,var4=varall.split (":")
print (var1,var2,var3,var4)

Try This ..it will create list of strings and then store them in var1,var2,var3,var4...make sure you have only 4 words separated by ":"..