How to assign multiple strings to a variable in python

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

Example

x, y, z = "Orange", "Banana", "Cherry"
print[x]
print[y]
print[z]

Try it Yourself »

Note: Make sure the number of variables matches the number of values, or else you will get an error.

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

Unpack a Collection

If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.

Example

Unpack a list:

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print[x]
print[y]
print[z]

Try it Yourself »

Learn more about unpacking in our Unpack Tuples Chapter.


So I have tried searching this but apparently nobody ever needed to do this simple thing before?

I want to a variable to have multiple strings. so basically it is:

command = input[]
commands = "start" or "stop" or "help"

 while command.lower[] == commands:
    dosomething[]
 else
    dosomething[]

This is basically the idea, but it only take the first string which is "start" but ignores the other 2. I understand that it reads it as [ commands = "start" ] so I tried making it commands = "start" or commands = "stop" or commands = "help" but it flat out says it is wrong. so what did I do instead? Can someone help? Thanks

asked Dec 13, 2021 at 19:55

4

What you need is a list, then check for inclusion with if [not while which is a loop]

command = input[">>"]
commands = ["start", "stop", "help"]

if command.lower[] in commands:
    print["start/stop/help"]
else:
    print["other"]

answered Dec 13, 2021 at 19:59

azroazro

49.3k7 gold badges31 silver badges66 bronze badges

0

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types.

    The assignment operator[=] assigns the value provided to its right to the variable name given to its left. Given is the basic syntax of variable declaration:

    Syntax: var_name = value

    Example:

    a = 4

     Assign Values to Multiple Variables in One Line

    Given above is the mechanism for assigning just variables in Python but it is possible to assign multiple variables at the same time. Python assigns values from right to left. When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.

    While declaring variables in this fashion one must be careful with the order of the names and their corresponding value first variable name to the left of the assignment operator is assigned with the first value to its right and so on. 

    Example 1:

    Variable assignment in a single line can also be done for different data types.

    Python3

    a, b = 4, 8

    print["value assigned to a"]

    print[a]

    print["value assigned to b"]

    print[b]

    Output:

    value assigned to a
    4
    value assigned to b
    8

    Example 2:

    Not just simple variable assignment, assignment after performing some operation can also be done in the same way.

    Python3

    print["assigning values of different datatypes"]

    a, b, c, d = 4, "geeks", 3.14, True

    print[a]

    print[b]

    print[c]

    print[d]

    Output:

    assigning values of different datatypes
    4
    geeks
    3.14
    True

    Example 3:

    Assigning different operation results to multiple variable.

    Python3

    a, b = 8, 3

    add, pro = [a+b], [a*b]

    print[add]

    print[pro]

    Output:

    11
    24

    Example 4:

    Here, we are storing different characters in a different variables.

    Python3

    string = "Geeks"

    a, b, c = string[0], string[1:4], string[4]

    print[a]

    print[b]

    print[c]

    Output:

    G
    eek
    s

    How do I assign multiple values to a single variable in Python?

    Python Variables - Assign Multiple Values.
    ❮ Previous Next ❯.
    x, y, z = "Orange", "Banana", "Cherry" print[x] print[y] print[z] Try it Yourself ».
    x = y = z = "Orange" print[x] print[y] print[z] Try it Yourself ».
    Unpack a list: fruits = ["apple", "banana", "cherry"] x, y, z = fruits. print[x] print[y] ... .
    ❮ Previous Next ❯.

    How do you assign a list of values to a variable in Python?

    To initialize a list in Python assign one with square brackets, initialize with the list[] function, create an empty list with multiplication, or use a list comprehension. The most common way to declare a list in Python is to use square brackets.

    Can I assign 2 variables at once?

    Multiple variable assignment is also known as tuple unpacking or iterable unpacking. It allows us to assign multiple variables at the same time in one single line of code. In the example above, we assigned three string values to three variables in one shot. As the output shows, the assignment works as we expect.

    Can a variable have multiple values?

    A variable holds more than one value if you declare it to be of a composite data type. Composite Data Types include structures, arrays, and classes. A variable of a composite data type can hold a combination of elementary data types and other composite types. Structures and classes can hold code as well as data.

    Chủ Đề