rpiboy
Posts: 110
Joined: Mon Mar 20, 2017 8:39 pm

How to print without newline

Tue Jun 11, 2019 6:05 pm

Code: Select all

raise TypeError("""The sentence should be of type str
                                                  and not {}""".format(type(sentence)))
That code prints on two different lines. How do I get it to print on the same line?

Code: Select all

TypeError: The sentence should be of type str
                                                  and not <class 'int'>

PhatFil
Posts: 1246
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: How to print without newline

Tue Jun 11, 2019 6:48 pm

try

Code: Select all

raise TypeError("""The sentence should be of type str and not {}""".format(type(sentence)))
and remove the unwanted white spaces, tabs and cr or lf from between the 2 text elements

rpiboy
Posts: 110
Joined: Mon Mar 20, 2017 8:39 pm

Re: How to print without newline

Tue Jun 11, 2019 6:51 pm

PhatFil wrote:
Tue Jun 11, 2019 6:48 pm
try

Code: Select all

raise TypeError("""The sentence should be of type str and not {}""".format(type(sentence)))
and remove the unwanted white spaces, tabs and cr or lf from between the 2 text elements
I know I can put it on one line and it'll print without a newline. I am wondering how I can get the code to print on the same line when I have it on two separate lines?

ghp
Posts: 1352
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: How to print without newline

Tue Jun 11, 2019 7:29 pm

Hello, by using the triple quotes all whitespace and newline is in the string. You can't avoid it.
So either use a different statement, single line is already mentioned, or remove it.

Code: Select all

#remove whitespace
import re
raise TypeError( re.sub(r" *[\n] *", " ", 
                        """The sentence should be of type str
                                                  and not {}""".format(type(sentence))
                        )
                )

# concat lines
raise TypeError("The sentence should be of type str" +
                " and not {}".format(type(sentence)))

# single line, see comments above.
raise TypeError("The sentence should be of type str and not {}".format(type(sentence)))

User avatar
Paeryn
Posts: 2570
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How to print without newline

Tue Jun 11, 2019 9:52 pm

Or use the fact that two string literals with only whitespace between them are implicitly converted into a single string at compile time rather than at run time (and you can use different quotation marks for each part if you want).

Code: Select all

$ cat strings.py
print("This string is entered in two parts on separate lines "
        'but is really one string')

$ python3 strings.py
This string is entered in two parts on separate lines but is really one string
Or, using your original lines :-

Code: Select all

raise TypeError("The sentence should be of type str "
                                                  "and not {}".format(type(sentence)))
She who travels light — forgot something.

Aydan
Posts: 667
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: How to print without newline

Thu Jun 13, 2019 1:44 pm

Another way is to use a "\" before the linebreak like so:

Code: Select all

raise TypeError("The sentence should be of type str \
                                                  and not {}".format(type(sentence)))

johnlenon
Posts: 4
Joined: Mon May 06, 2019 6:36 pm

Re: How to print without newline

Sat Jun 15, 2019 2:16 pm

Following is the program referred from crazygeeks for printing without newline:

Code: Select all

# Python 3 code for printing without newline
  
print("Apple"), 
print("Pineapple") 
  
# array 
a = [1, 2, 3, 4, 5, 6, 7] 
  
# printing a element in same 
# line 
for i in range(7): 
    print(a[i]),  

Return to “Python”