0

I'm trying to enter user input into a string in two places in python 2.7.12 I want it to look something like this

    import os
    1 = input()
    2 = input()
    print os.listdir("/home/test/1/2")

I know you can use .format() to input into string but the only way I know how to do it is

    print os.listdir("/home/test/{0}".format(1))

but I couldn't figure out how to enter a second input into the string. sorry for any confusion, I'm kinda new to Stack Overflow. If you have any questions please ask.

1 Answer 1

4
import os
segment1 = input()
segment2 = input()
print os.listdir("/home/test/{}/{}".format(segment1, segment2))

1 and 2 are not legal variable names, so 1 = input() will cause an error.

You can use as many variables as you want in your format string; just pass them as additional parameters to .format(...). In the format string, you can use {0}, {1}, etc., or you can just use {} positionally. (The first {} refers to the first parameter, the second {} to the second parameter, etc.).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.