w3resource
Python Exercises

Python: Split a variable length string into variables

Python Basic: Exercise-131 with Solution

Write a Python program to split a variable length string into variables.

Sample Solution:-

Python Code:

var_list = ['a', 'b', 'c']
x, y, z = (var_list + [None] * 3)[:3]
print(x, y, z)
var_list = [100, 20.25]
x, y = (var_list + [None] * 2)[:2]
print(x, y)

Sample Output:

a b c                                                                                                         
100 20.25 

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to use double quotes to display strings.
Next: Write a Python program to list home directory without absolute path.

What is the difficulty level of this exercise?