0

I'm very new to python and I am making a text based game. In the start I ask the users name. How would I add what the user input to when I "print"?

print ("Welcome to my first game! It's not much but it's mine.")
print ("What is your name")
name = input()

In the code above how would I add the users name into the next line? For example It would look something like this:

print ("Hi """insert name here""", lets start!")

I hope I made this clear as I don't have a large enough Python vocabulary to make my ideas clear

Marc Spiegel
  • 11
  • 1
  • 1
  • 1
  • Sounds like you need to run through a tutorial, not jump into creating something. This is one of the most fundamental building blocks of most tutorials. Go through one! – Adam Smith Sep 10 '15 at 01:05

2 Answers2

0

You can use string formatting to output what you desire:

>>> print("Hi {name}! Let's begin".format(name=name))
Hi Andy! Let's begin

This is using a named argument (name) and assigning your variable to it. You could also do something like this:

>>> print("Hi {}! Let's begin".format(name))
Hi Andy! Let's begin

This is using just a positional argument. If you have multiple fields to fill in, put them in the order they should appear:

>>> print("Hi {}! Let's {}".format(name, 'begin'))
Hi Andy! Let's begin

>>> print("Hi {}! Let's {}".format(name, 'quit'))
Hi Andy! Let's quit
Andy
  • 49,085
  • 60
  • 166
  • 233
-1

Here's one way to do it:

print("Welcome to my first game! It's not much but it's mine.")
name = input("What is your name?")
print("Hi ", name, ", let's start!", sep="")

Here's another way using .format:

print("Welcome to my first game! It's not much but it's mine.")
name = input("What is your name?")
print("Hi {} let's start!".format(name)) # Will replace "{}" with name
RobertR
  • 745
  • 9
  • 27
  • This question is tagged with python 3.4. In Python 3, `raw_input` has been renamed to `input` - See [What's New in Python 3.0](https://docs.python.org/3/whatsnew/3.0.html#builtins) and [PEP3111](https://www.python.org/dev/peps/pep-3111/) – Andy Sep 10 '15 at 18:46
  • Oh whoops! I haven't really made the change to python 3.x official yet and was unaware of that change. – RobertR Sep 11 '15 at 02:45