Print more than one value [duplicate] - python

This question already has answers here:
How can I print multiple things (fixed text and/or variable values) on the same line, all at once?
(13 answers)
Printing variables in Python 3.4
(6 answers)
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
How to print formatted string in Python3?
(6 answers)
Closed 2 years ago.
C++
std::cout << "Hello world!";
// output: Hello world!
Python
print("Hello world!")
# output: Hello world!
That works. But how can I do this in Python?
std::string name = "Robert";
std::cout << "Hello " << name << ", how are you?";

Just use commas to seperate arguments:
print("Hello ", name, ", how are you?", sep='')
You can also use the f string formatter:
print(f"Hello {name}, how are you?")
or also with str.format():
print("Hello {}, how are you?".format(name))

You can try this:-
name = 'Robert'
print(f'Hello {name}, how are you?')
OR
print('Hello ', name, ', how are you?', sep='')
Output:-
Hello Robert, how are you?

name = 'Robert'
print('Hello ', name,', how are you?',sep = '')
sep - separator parameter, which will eliminate the space between name and comma.

Related

Print words within the string from last to first [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed last year.
for example I entered follwoing string ;
" hello I am Mohsen" ;
now I want to Print on output :
"Mohsen am I hello "
please help me !
Both Corralien's and Tobi208's works, and can be combined to the shorter version;
s = "hello I am Mohsen"
print(' '.join(s.split(' ')[::-1]))
or if you want to input the string in the terminal as a prompt;
s = input()
print(' '.join(s.split(' ')[::-1]))
Split by space, reverse the list, and stitch it back together.
s = " hello I am Mohsen"
words = s.split(' ')
words_reversed = words[::-1]
s_reversed = ' '.join(words_reversed)
print(s_reversed)

How do I trim whitespace from a string(right left and between spaces)? [duplicate]

This question already has answers here:
Remove all whitespace in a string
(14 answers)
Closed 2 years ago.
I want to remove any extra whitespace in a sentece how can i do this with python?
for ex :
(" hello world") >>> ("hello world")
str = " hello world "
str_clean = ' '.join(str.split())
print(str_clean)
hello world
Edited, I think I overlooked your initial text.

Printing strings with print() in Python [duplicate]

This question already has answers here:
Why is my print function printing the () and the "" along with the statement?
(2 answers)
Closed 5 years ago.
I am learning Python and I run into a syntax problem. When I try to create a function that prints "Hello (name)", the quotation marks and the comma appear alongside the string.
For example:
def sayHello(name = 'John'):
print('Hello ', name)
sayHello()
prints as:
('Hello ', 'John')
Any idea why it's the case?
Thanks!
You code would work as expected in Python 3.
Python 2 uses print statement, i.e command, rather than function.
The command understands your argument as a tuple (pair).
Correct use of print command in Python 2
print 'Hello,' name
Alternatives are
print 'Hello, %s' % name
See Using print() in Python2.x
for details
In python, () means a tuple. It will print "()" if its empty, and "(value1, value2, ...)" if it contains values.
In your example, you print a tuple which contains two values "Hello" and name.
If you want to print "Hello (name)", you could try:
print "Hello ", name
print "Hello " + name

Python : Input, Raw input error [duplicate]

This question already has answers here:
"NameError: name '' is not defined" after user input in Python [duplicate]
(3 answers)
Closed 6 years ago.
I am trying a simple Hello world, this is my code-
def hello(name=''):
if len(name) == 0 :
return "Hello, World!"
else :
return "Hello, %s!" %(name)
my_name = raw_input()
x = hello(my_name)
print (x)
This code works fine if I use raw_input, but if I use input, it gives an error.
Doesn't the new python not support raw_input.
I also want to know why I defined the parameter in my function as following-
def hello(name='')
Why did I need to use the '' after name
I am really confused, please help. If you have any advice to improve my program, it's appreciated
If you are passing string with input, you have to also mention the double quotes ", for example "My Name"
Whereas in raw_input, all the entered values are treated as string by default
Explanation:
# Example of "input()"
>>> my_name = input("Enter Name: ")
Enter Name: "My Name"
# Passing `"` with the input, else it will raise NameError Exception
>>> my_name
'My Name' <--- value is string
# Example of "raw_input()"
>>> my_name = raw_input("Enter Name: ")
Enter Name: My Name
# Not passing any `"`
>>> my_name
'My name' <--- still value is string

Replace not working in python [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 7 years ago.
While trying basic python scripting in eclipse IDE, I am encountering problem:
I just tried a simple code, which I found online:
var = 'hello , world'
print "%s" % var
var.strip(',')
print "%s" % var
The result i am getting is
hello , world
hello , world
Also i tried with replace command, but the result remain unchanged
var = 'hello , world'
print "%s" % var
var.replace(',', '')
print "%s" % var
The result obtained is
hello , world
hello , world
I could not figure out were I am making mistake.
In Python, strings are immutable, change:
var.replace(',', '')
to
var = var.replace(',', '')
Strings in Python are immutable. That means any methods that operate on them don't change the value, they return a new value.
What you need is
var = 'hello , world'
print "%s" % var
var = var.replace(',') # Replace the variable.
print "%s" % var
Also, print "%s" % var is redundant. You can just use print var.

Categories

Resources