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() + "*/"
Related
This question already has answers here:
Efficient way to add spaces between characters in a string
(5 answers)
Closed 1 year ago.
So i am making a very simple python program which takes a string as input from user and adds a '-' between every character but the problem is it adds it to last one too but i don't want that...
Here's the code:
string = input("Enter the string to be traversed: ")
for ch in string:
print(ch , end = "-")
Output:
Enter the string to be traversed: helloworld
h-e-l-l-o-w-o-r-l-d-
I don't want it to print '-' after the last character...
This is a common problem and there is a string method to deal with it. Instead of a for loop, join the characters with a dash.
string = input("Enter the string to be traversed: ")
print("-".join(string))
.join will work with anything that iterates strings. In your case, a string does that, character by character. But a list or tuple would work as well.
This question already has answers here:
Better way to remove multiple words from a string?
(5 answers)
Closed 3 years ago.
Note: Without chaining replace method (or) looping the characters in for loop (or) list comprehension
input_string = "the was is characters needs to replaced by empty spaces"
input_string.replace("the","").replace("was","").replace("is","").strip()
output: 'characters needs to replaced by empty spaces'
Is there any direct way to do this?
You can use python regex module(re.sub) to replace multiple characters with a single character:
input_string = "the was is characters needs to replaced by empty spaces"
import re
re.sub("the|was|is","",input_string).strip()
'characters needs to replaced by empty spaces'
This should help..
input_string = "the was is characters needs to replaced by empty spaces"
words_to_replace=['the', 'was','is']
print(input_string)
for words in words_to_replace:
input_string = input_string.replace(words, "")
print(input_string.strip())
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:
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!