Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to change what are in variable slots to no success

    I have been learning to use the 3.5.2 python shell and have been confused about how python stores variables in memory. After I change what is stored in some variables, how do I make change what is in the empty variable slots in a string variable?

    I first assign three integers to three variables.

    >>>num1 = 6
    >>>num2 = 7
    >>>num3 = 8

    I then assign a string to a variable that has three empty variable slots.

    >>>list_of_numbers = "Here is a list of three numbers: {}, {}, and {}"

    I then use the format function to place the three integers in to the empty variable slots.

    >>>list_of_numbers= list_of_numbers.format(num1, num2, num3)

    When I ask for a read out of list_of_numbers I, of course, get:

    >>>list_of_numbers
    'Here is a list of three numbers: 6, 7, and 8'

    The problem arises when I assign new integers to num1, num2, and num3.

    >>>num1 = 12
    >>>num2 = 14
    >>>num3 = 28

    Because I have changed what is stored in num1, num2, and num3, I attempt to use the format function to assign the new variables to the string list_of_numbers.

    >>>list_of_numbers = list_of_numbers.format(num1, num2, num3)

    When I ask for a read out of list_of_numbers I get this:

    >>>list_of_numbers
    'Here is a list of three numbers: 6, 7, and 8'

    Why doesn't the interpreter change what what variables are filled in list_of_numbers? I know the CPU has assigned new integers to the variables num1, num2, and num3. So why can't I change what variables are listed in list_of_numbers?

  2. #2
    Regular Coder
    Join Date
    Oct 2015
    Posts
    383
    Thanks
    1
    Thanked 45 Times in 42 Posts
    When you try to change the string the second time, it equals
    'Here is a list of three numbers: 6, 7, and 8' and not 'Here is a list of three numbers: {}, {}, and {}' so the interpreter does not see where to make the replacements.

    Code:
    >>>num1 = 6
    >>>num2 = 7
    >>>num3 = 8
    >>>list_of_numbers = "Here is a list of three numbers: {}, {}, and {}"
    >>>list_of_numbers= list_of_numbers.format(num1, num2, num3)
    >>>list_of_numbers
    'Here is a list of three numbers: 6, 7, and 8'
    
    >>>num1 = 12
    >>>num2 = 14
    >>>num3 = 28
    >>>list_of_numbers = "Here is a list of three numbers: {}, {}, and {}" # reset the string with placeholders
    >>>list_of_numbers = list_of_numbers.format(num1, num2, num3)
    >>>list_of_numbers
    'Here is a list of three numbers: 12, 14, and 28'


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •