This question already has answers here:
Print without space in python 3
(6 answers)
Closed 2 years ago.
print("Blah: [",blah_url,blah,"]<br>", file=f)
Output:
Blah: [ http://blah/Pages/Revision.aspx?projectId=541511cd-86bf-46fc-ae70-aec46e6030c7 blah ]<br>
The additional space after the open bracket "[" and before the close bracket "]" shouldn't be there.
It should look like this:
Blah: [http://blah/Pages/Revision.aspx?projectId=541511cd-86bf-46fc-ae70-aec46e6030c7 blah]<br>
I have confirmed there are no extra spaces in the data being supplied as the input.
The print function automatically adds spaces between each of the parameters. Try this (it overrides the adding of spaces):
print("Blah: [", blah_url, blah, "]", file=f, sep="")
or this (passing it in as one parameter):
print("Blah: [" + blah_url + blah + "]", file=f)
Related
This question already has answers here:
Printing each item of a variable on a separate line in Python
(4 answers)
Closed 1 year ago.
Input:
a = input("enter-")
b = input("enter-")
c = input("enter-")
print(a, "\n", b, "\n", c, "\n")
Output:
enter-line1
enter-line2
enter-line3
line1
line2
line3
How to remove the space before line2 and line3?
You can use the argument sep of print. By default, the separator is space:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
>>> print(a, b, c, sep='\n')
line1
line2
line3
The print() function has more arguments than string and looks like this
print(object(s), sep=separator, end=end, file=file, flush=flush)
Default separator is ' ' so space will be added between every element.
To print without spaces, you need to use it like this
print(a,"\n",b,"\n",c,"\n", sep='')
Using the print statement with Python and commas in between will naturally give spaces. For example, if I used print(a,b,c), then the output would be line1 line2 line3. Here, by using print(a,"\n",b,"\n",c,"\n"), you are putting a space after you put a newline. So either use separate print statements or use the "\n" as the separator between the arguments without using commas.
This question already has answers here:
writing double quotes in python
(3 answers)
Closed 3 years ago.
I want to print a string which contains " in it. I need to use row["column_name"] also inside the print function. I have problem printing " in the middle of string
print("_:m"+str(row["movieId"])+" <release_year> "+str(row["release_year"])+"^^<datetime> .\n")
expected output is
_:m3 <release_year> "1995"^^<datetime> .
You could use backslash followed by a quote
Print( ' \" ')
Output:
"
You can escape the " or other chars with \" in print statement
>>> print("_:m" + "\"1995\"")
_:m"1995"
I hope this helps.
https://docs.python.org/3/reference/lexical_analysis.html#literals
This question already has answers here:
In Python, is it possible to escape newline characters when printing a string?
(3 answers)
Closed 7 years ago.
I have a delim list . Now I want to print every element in delim list. But print funtion in python is printing everything except character like '\t' , '\n'. I know it is usual . But can I print this like normal characters or strings.
delim=['\t','\n',',',';','(',')','{','}','[',']','#','<','>']
for c in delim:
print c
It is giving output :
it is printing all the list skipping \t and \n
Change them to raw string literals by prefixing a r:
>>> print '\n'
>>> print r'\n'
\n
For your example this would mean:
delim=[r'\t',r'\n',',',';','(',')','{','}','[',']','#','<','>']
for c in delim:
print c
If you just want to print them differently use repr
for c in delim:
print repr(c)
Note: You will also see additional ' at the beginning and end of each string.
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 7 years ago.
I have this code
print "/*!",your_name.upper(),"*/";
where your_name is the data the user inputs.
How can I edit the code above to tell the system to remove any whitespace?
UPDATE:
If i print the code, i'll get
/*! your_name */
I want to remove the whitspaces between /*! your_name */
The spaces are inserted by the print statement when you pass in multiple expressions separated by commas. Don't use the commas, but build one string, so you pass in just the one expression:
print "/*!" + your_name.upper() + "*/"
or use string formatting with str.format():
print "/*!{0}*/".format(your_name.upper())
or the older string formatting operation:
print "/*!%s*/" % your_name.upper()
Or use the print() function, setting the separator to an empty string:
from __future__ import print_function
print("/*!", your_name.upper(), "*/", sep='')
The white spaces are inserted by print when you use multiple expressions separated by commas.
Instead of using commas, try :
print "/*!" + your_name.upper() + "*/"
This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 9 years ago.
I have my data as below
string = ' streptococcus 7120 "File being analysed" rd873 '
I tried to split the line using n=string.split() which gives the below result:
[streptococcus,7120,File,being,analysed,rd873]
I would like to split the string ignoring white spaces in " "
# output expected :
[streptococcus,7120,File being analysed,rd873]
Use re.findall with a suitable regex. I'm not sure what your error cases look like (what if there are an odd number of quotes?), but:
filter(None, it.chain(*re.findall(r'"([^"]*?)"|(\S+)', ' streptococcus 7120 "File being analysed" rd873 "hello!" hi')))
> ['streptococcus',
'7120',
'File being analysed',
'rd873',
'hello!',
'hi']
looks right.
You want shlex.split, which gives you the behavior you want with the quotes.
import shlex
string = ' streptococcus 7120 "File being analysed" rd873 '
items = shlex.split(string)
This won't strip extra spaces embedded in the strings, but you can do that with a list comprehension:
items = [" ".join(x.split()) for x in shlex.split(string)]
Look, ma, no regex!