Skip to content
Sign upLog in
← Back to Community

does anyone know how to make it print a sentence with persons name in it

Profile icon
yomicroyeewww

i want to make it orint a persons name with a sentence in it

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
yomicroyeewww
Comments
hotnewtop
Profile icon
Coder100

Easy! There are many ways to do this.

1. fstrings

import time print('welcome young fellow replers how are you guys today?') name = input() print(name) time.sleep(1) print(f'it is nice to meet you {name}.')

2. addition

import time print('welcome young fellow replers how are you guys today?') name = input() print(name) time.sleep(1) print(f'it is nice to meet you '+name+'.')

3. Using extra args

import time print('welcome young fellow replers how are you guys today?') name = input() print(name) time.sleep(1) print(f'it is nice to meet you ', name, '.', sep="")

4. String interpolation

Use %s to insert a string, and %d to insert a number.

import time print('welcome young fellow replers how are you guys today?') name = input() print(name) time.sleep(1) print(f'it is nice to meet you %s.' % (name))

5. .format

import time print('welcome young fellow replers how are you guys today?') name = input() print(name) time.sleep(1) print(f'it is nice to meet you {}.'.format(name))
Profile icon
RYANTADIPARTHI

Solution

There are multiple ways to do it. here are some of them:

  • 1st one is the normal print with a ,
import time print('welcome young fellow replers how are you guys today?') name = input() print('ok', name) time.sleep(1) print('it is nice to meet you.')
  • 2nd way is formatting it:
import time print('welcome young fellow replers how are you guys today?') name = input() print("nice {}".format(name)) time.sleep(1) print('it is nice to meet you.')

IT can be used in multiple variables

import time print('welcome young fellow replers how are you guys today?') name = input() print("nice {}, this is cool {}".format(name, name)) time.sleep(1) print('it is nice to meet you.')
  • 3rd, f This is in more advanced versions of python, and is advanced of format.
import time print('welcome young fellow replers how are you guys today?') name = input() print(f"Cool {name}") time.sleep(1) print('it is nice to meet you.')

You just have to use a {} and put f before the string

  • 4th is the %s
import time print('welcome young fellow replers how are you guys today?') name = input() print("nice %s", %(name)) time.sleep(1) print('it is nice to meet you.')
  • and the + for this, put spaces, to look good
import time print('welcome young fellow replers how are you guys today?') name = input() print("cool " + name) time.sleep(1) print('it is nice to meet you.')

I put an extra space before it, so it would not be concatenated close together.


Well that's it. You can use either of them, but the most easiest way is just a ,. It's more viable. But it's your choice. any of them work.

Profile icon
[deleted]

I am assuming what you mean to ask is: when we take the name from the user using input statement, how can we print it back. Here's how:


name = input("What is your name?")
print("Hello " + name)


Output:
Hello (name of the person)

Please mark this as the answer if this is what you were looking for! Thanks!

Profile icon
Bookie0

@dhruvkumarrajan
add code blocks and syntax highlighting with the backslashes (`):

# your code here name = input("What is your name?") print("Hello " + name) # wow colors!

Screen Shot 2020-12-01 at 9.19.17 AM

Profile icon
Coder100

no asking for marking as correct answer

@dhruvkumarrajan

Profile icon
[deleted]

@Coder100

oops... Sorry

Profile icon
Coder100

no problem, now you know!

@dhruvkumarrajan

Profile icon
JBloves27

I think it would be better if you were more descriptive, but if you mean f strings, here it is:

# Here it is: name = input("what is your name?") print(f"Hello {name}!") # Output: What is your name? input name here. # Output: Hello name # This works, or you can do this:
# This works as well: name = input("What is your name?") print("Hello",name,"!") # Output is same # Another way:
name = input("what is your name?") print("Hello"+name+"!") # output is same

Last one:

name = input("what is your name?") print("Hello %s!" % name) # output is same

I hope this helped!

Profile icon
Wumi4

You can use the + operator.

# Here's an example print("Hi" + "user!") # Output: Hi user!
Profile icon
[deleted]

@Wumi4

I think he means with input from the user.

Profile icon
Wumi4

@dhruvkumarrajan
Then just replace the string with the input value!

name = input("Name?") print("Hello" + name)
Profile icon
[deleted]

@Wumi4

yes :)