Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone , please can you show me how we print the comma near a word in python with the input function , is there another way than the method format , i want to print "hello Mohamed, you are 20 years old " as i sad the comma near Mohamed because i tried with it , it print "hello Mohamed , you are 20 years old" there is space between the comma and Mohamed.

What I have tried:

searching on the net and coding on the text editor
Posted
Updated 16-Sep-21 5:50am
Comments
Richard MacCutchan 16-Sep-21 11:35am    
Maybe if you showed the code that you used we could tell you what the problem is.
Mohamed AIT HADDOU 16-Sep-21 12:35pm    
Name = input("Enter your name : ")
Age = int(input("Enter your age : "))

print("Hello",Name,", you are",Age,"years old")
Richard MacCutchan 16-Sep-21 12:43pm    
The problem is that print will always add a space between fields. You can suppress that by adding sep='' to the print statement, but you would then need to add the spaces for all the other fields. A better solution is to use format strings as described at string — Common string operations — Python 3.9.7 documentation[^].
Mohamed AIT HADDOU 16-Sep-21 12:46pm    
Thank you a lot

Concatenate the the message into a single string:
Python
name = input('Enter your name: ')
age = input('Enter your age: ')
print ('hello ' + name + ', you are ' + age + ' years old')
 
Share this answer
 
Comments
Mohamed AIT HADDOU 16-Sep-21 12:30pm    
thank you dear friend is there another method more specific or better that i should know ?
[no name] 17-Sep-21 4:40am    
print(f"Hello {Name} You are {Age} Years Old")

print("Hello %s You are %d Years Old" %(Name,Age))
You can try as you like


print(f"Hello {Name} You are {Age} Years Old")

print("Hello %s You are %d Years Old" %(Name,Age))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900