0

I am trying to learn Python and an assignment I am doing has the following prompt. I'm in the third chapter and so far this chapter has gone over strings, lists, tuples, sets, dictionaries, data type and type conversions.

Write a single statement to print: user_word,user_number. Note that there is no space between the comma and user_number.

The inputs I am supposed to test with are Amy and 5

The code is prewritten and below. I am not allowed to edit the first two lines. The corrections/working code must be added afterwards.

    user_word = input()
    user_number = int(input())
    
    ''''Your Solution goes here''''
    

So far I have added to where Your Solution goes here is

    print((user_word), (user_number))

When I added this and used Amy and 5 as inputs my output is almost identical. But It needs to show as Amy,5 with a comma and no spaces. The problem is , is the only way I know to separate the variables, comma also adds a space by default giving me Amy 5 and this is not the solution I need. [] and curly {} seem to be used for multiple different operations and the context seems to determine how it works. The comma will not show. even when I add it.

   print((user_word), ',', (user_number))

This goes and adds a bunch of spaces also. If I use apostrophe ' it prints the string and not the value of the variables the user typed in. If I remove all the spaces and commas I get errors after the IDLE interpreter runs. Also I can't eve get a . to show up right after the last letter there is always a space.

I also tried to use

   print('{:s}'.format(user_word), '{:d}'.format(user_number)) 

but again it seems to miss the comma. The "user" the program that my work is ran through is never going to add "Amy,".

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70

1 Answers1

1

You can use an fstring, which is a string that allows you to format it very easily, like this -

print(f"{user_word},{user_number}")

or you can use string concatenation, like this -

print(user_word + "," + user_number)

another way is to set the print separator string to an empty string

print(user_word, ",", user_number, sep='')

Anyways, I suggest you read about f-string and string formatting in general.

alonkh2
  • 533
  • 2
  • 11