0

Im given a question where I have to ask the user for his/her address then split to a new line where there is a comma in the address. After doing that align the whole thing to right, ive been trying to figure this out but I can only do one of the 2, split or align. This is my code:

def Q5():
    str = input("Enter your address (separate lines with comma) :\n")

    for c in str:
        print(c, end="")
        if(c == ","):
            print("")


    #print (str.rjust(50))

Q5()

Please help me fix this. Thanks in advance

3
  • Can you show an example of what you expect as your output? Commented Oct 18, 2015 at 14:46
  • okay so what I exactly want is, try remove the entire for loop and remove the comment from the commented line. When you print the input will be aligned to the right, but what I also want is the input to be aligned to the right and also there should be a new line each time there is a comma in the string. Commented Oct 18, 2015 at 14:56
  • Hint: take a look at the str.split() method. BTW, please don't use str as a variable name as that shadows the built-in str type. Commented Oct 18, 2015 at 14:57

1 Answer 1

1

Python has a split function, which takes the splitting character as an argument:

x = "this,is,a,string"
split_string = x.split(",")
print split_string

returns

['this', 'is', 'a', 'string']

which is an array that contains all of the words. You want to right-align all of them, so that would be

right_aligned = [str.rjust(50) for str in split_string]

Then these can be joined by newlines:

"\n".join(right_aligned)

returns

                                          this
                                            is
                                             a
                                        string
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.