1

I'm still only learning the basics of Python, but my code is this:

name = input("Hi, what is your name? ")
print("Hi,", str(name), ". We need to check your funds for all your drinking,", name, ".")

When I run it, I type in 'Bojack' (without the quote marks) but it'll always come back with:

Hi, Bojack . We need to check your funds for all your drinking, Bojack .

How do I resolve this?

Jabberminor
  • 11
  • 1
  • 2

3 Answers3

2

You can print strings like this:

print("My name is {name}.".format(name=name))
foxyblue
  • 2,859
  • 2
  • 21
  • 29
2

The problem isn't (as you seem to think) that name is being assigned 'Bojak ' (with a space) - the problem is that when you print() like that python3 separates the arguements by default with a space.

you can override that by passing the sep arguement:

print("Hi,", str(name), ". We need to check your funds for all your drinking,", name, ".", sep="")
Stael
  • 2,619
  • 15
  • 19
0

The reason why this is occurring is that you are joining the strings with a comma (,). A way to avoid this from happening is to use string concatenation:

name = input("Hi, what is your name? ")
print("Hi, " + name + ". We need to check your funds for all your drinking, " + name + ".")

Output:

Hi, Bojack. We need to check your funds for all your drinking, Bojack.

While this solution is not the most efficient and clean, it is easy to understand for Python beginners. For faster solutions, string formatting is better.

Hope this helped!

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27