how to make space between 2 string in python 3? - python

hi i want to ask if how i can put one line space in python 3.
i use this code:
#def space():
#print()
but when i want to print it there is a "none" that prints for me but i want just a line with nothing
i.e i want to type :
"hi how are you :
iam fine"
i want to have the space between "hi how are you " and "iam fine"
i give this to python3 :
print('hi how are you",space(),"iam fine")
but it will give me only:
hi how are you None iam fine ..
what is the problem here? what dose this none means?

print() happens immediately. If you want a newline then you need to return '\n' instead.

this is what you want:
def space():
return '\n'
None is what it sounds like, it's printing None because you're not giving print() any inputs

Related

How end="" with the string print statement print in same line

Everyone all of you. I am learning python right now.
In python, As you know that when we print anything using print() function it prints line by line like this:
print("Hello ")
print("World!")
When we do this then output would be:
Hello
World!
but if we want to be in same line we add end="" like this:
print("Hello ", end="")
print("World!")
Its output:
Hello World!
But I have curiosity that how does it exactly works. Why it comes in same line. The work of end="____" is to add some texts after sentences or words.
Please explain me why it comes in same line and what is the reason.
Thanks in advance!
'end' tells the print function what to end your text with. Think of it as appending that string. 'end' defaults as '\n' meaning that a new line is appended to your string by default. So, if you change it to "", you are telling it not to alter or append to your string. Which means you simply print "Hello " instead of "Hello \n"
print ("Hello, Python")
The preceeding line will print to the console "Hello, Python\n" '\n' is a new line and it is the default behavior for the print function. It will by default always add '\n' to your string. If you supply 'end' to the function, it will print that instead of '\n'.
For example, the following two lines are identical in behavior:
print ("Hello, Python")
print ("Hello, Python",end="\n")
You could however provide your own 'end' to append a different string, if you so chose. For example:
print ("Hello, Python",end="!")
The preceeding line will print, "Hello, Python!"
For more information regarding new lines, check out the following:
https://wtmatter.com/python-new-line/
It is because In python when you print statement there is end="\n" as default in the python which allow it to print in another line but you will not see it. When you put end="" then it will replace with \n which print in different line and then it prints in same line. Hope it will help

Python if statement not executing

# TEST
import sys
a=sys.stdin.readline() # here the user inputs the string "HELLO"
print a
if a == "HELLO":
sys.stdout.write("GOOD_BYE")
print "AAAAAAAAAAA"
raw_input('\npress any key to continue')
Hi there. I am new to Python.
I am using Python 2.7.11.
I do not understand why control is not entering the if statement.
The output for the given code comes out as
HELLO
HELLO
AAAAAAAAAAA
press any key to continue
NOTE: The first "HELLO" above is user input
I've tried sys.stdout.flush() for the sys.stdout.write() statement. But it doesn't seem to help.
If I write the same code with a=raw_input() instead of the second line, it works perfectly fine.
Can anyone explain the reason for this.
readline comes with a newline character at the end. So what you are actually doing is comparing
HELLO\n == HELLO
which is actually false.
Do a.rstrip() to remove newline.
readline() function read newline character from standard input console. Please use rstrip("\n") to remove the same.
import sys
a=sys.stdin.readline().rstrip('\n')
print (a)
if a == "HELLO":
sys.stdout.write("GOOD_BYE")
print ("AAAAAAAAAAA")
raw_input('\npress any key to continue')
Try using 'in' instead of '==' in your if condition, sometimes the lines might have some hidden characters.
if "HELLO" in a:
You are pressing 'ENTER' after input string to send input. So your input is 'HELLO\n' while your if statement 'if' condition is 'a == "HELLO"'.
Use strip() method. The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).
So new working code :
import sys
a=sys.stdin.readline().rstrip('\n')
a=a.strip() # This will remove \n from input
print (a)
if a == "HELLO":
sys.stdout.write("GOOD_BYE")
print ("AAAAAAAAAAA")
raw_input('\npress any key to continue')

Invalid Syntax with name input

I'm new to programming and Python. I am attempting to write my first program, however I keep getting an "Invalid Syntax" error for these few lines of code. This is all I am trying to get down pat. When I written this, it was in IDLE for Python 3.6.1
name = raw_input ("Please tell me your name: ")
print ("Hello "+ name)
print "Your name is:" + str(len(name)) "letters long"
Can you guys please give me a hand for this? Also I like more information around Syntax errors because I heard they happen often. What are they?
I think you lost a plus in your code,
name = raw_input ("Please tell me your name: ")
print ("Hello "+ name)
print "Your name is:" + str(len(name)) "letters long"
# ^ There should be a "+" ?
The right code should be:
name = raw_input ("Please tell me your name: ")
print ("Hello "+ name)
print "Your name is:" + str(len(name)) + "letters long"
There are a few things wrong with it from the beginning, I'd recommend you check out A Byte of Python as it gives a solid understanding of Python and caters it to newbies.
As for your actual issue print is no longer a keyword in Python 3 and requires the use of parenthesis print("thing") as opposed to print "thing"
A second issue I spotted is that raw_input is now input in Python 3 so that also needs to be changed.
To avoid the final issue which seems to be the lack of a + when you concatenate the string, use the str.format() method or perhaps Python 3.6's f strings.
I had something similar happen, when I added a script to a /bin directory.
my issue was that at the start I had:
#!/usr/bin/env python
that called python2
when I needed:
#!/usr/bin/env python3
that actually calls a python3 environment

Replacing more than one line in the console in python

I'm writing a program in python and I'd like to replace more than one line in the console with new text.
For example if I have 3 sentences printed to the console with:
print("Hello World!")
print("How are you!")
print("What's going on?")
Where each on is on a different line (and so has an \n).
How do I go about replacing all of this text when it displays in the console? I can't us \r in this situation due to the \n.
This is kind of an old post, but I came across it and worked out a solution as well. Added a timer, because otherwise the print statements bury each other and you'll only be able to read the last one. I'm on python 2.7:
import os
import time
os.system("printf 'Hello World!'")
time.sleep(1)
os.system("printf '\rHow are you?!'")
time.sleep(1.5)
os.system("printf '\rWhats going on?'")
os.system("echo ")
A simple fix would be to simply change the end separator for printing your strings. you can specify how you want the print function to separate calls with the end argument
print("hello world!", end="")
print("\rhello world again!")
In this case, we're setting the separator to "", which is nothing. So printing the next strings starts on the same line thus \r can be used. Compiling that gives you hello world again! on one line.

Python: avoid new line with print command [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 9 years ago.
When I use the print command, it prints whatever I want and then goes to a different line. For example:
print "this should be"; print "on the same line"
Should return:
this should be on the same line
but instead returns:
this should be
on the same line
More precisely I was trying to create a program with if that told me whether a number was a 2 or not
def test2(x):
if x == 2:
print "Yeah bro, that's tottaly a two"
else:
print "Nope, that is not a two. That is a (x)"
But it doesn't recognise the last (x) as the value entered, and rather prints exactly: "(x)" (the letter with the brackets). To make it work I have to write:
print "Nope, that is not a two. That is a"; print (x)
And if e.g. I enter test2(3) that gives:
Nope, that is not a two, that is a
3
So either I need to make Python recognise my (x) inside a print line as the number; or to print two separate things but on the same line.
IMPORTANT NOTE: I am using version 2.5.4
Another note: If I put print "Thing" , print "Thing2" it says "Syntax error" on the 2nd print.
In Python 3.x, you can use the end argument to the print() function to prevent a newline character from being printed:
print("Nope, that is not a two. That is a", end="")
In Python 2.x, you can use a trailing comma:
print "this should be",
print "on the same line"
You don't need this to simply print a variable, though:
print "Nope, that is not a two. That is a", x
Note that the trailing comma still results in a space being printed at the end of the line, i.e. it's equivalent to using end=" " in Python 3. To suppress the space character as well, you can either use
from __future__ import print_function
to get access to the Python 3 print function or use sys.stdout.write().
In Python 2.x just put a , at the end of your print statement. If you want to avoid the blank space that print puts between items, use sys.stdout.write.
import sys
sys.stdout.write('hi there')
sys.stdout.write('Bob here.')
yields:
hi thereBob here.
Note that there is no newline or blank space between the two strings.
In Python 3.x, with its print() function, you can just say
print('this is a string', end="")
print(' and this is on the same line')
and get:
this is a string and this is on the same line
There is also a parameter called sep that you can set in print with Python 3.x to control how adjoining strings will be separated (or not depending on the value assigned to sep)
E.g.,
Python 2.x
print 'hi', 'there'
gives
hi there
Python 3.x
print('hi', 'there', sep='')
gives
hithere
If you're using Python 2.5, this won't work, but for people using 2.6 or 2.7, try
from __future__ import print_function
print("abcd", end='')
print("efg")
results in
abcdefg
For those using 3.x, this is already built-in.
You simply need to do:
print 'lakjdfljsdf', # trailing comma
However in:
print 'lkajdlfjasd', 'ljkadfljasf'
There is implicit whitespace (ie ' ').
You also have the option of:
import sys
sys.stdout.write('some data here without a new line')
Utilize a trailing comma to prevent a new line from being presented:
print "this should be"; print "on the same line"
Should be:
print "this should be", "on the same line"
In addition, you can just attach the variable being passed to the end of the desired string by:
print "Nope, that is not a two. That is a", x
You can also use:
print "Nope, that is not a two. That is a %d" % x #assuming x is always an int
You can access additional documentation regarding string formatting utilizing the % operator (modulo).

Categories