print special characters like " as output to the terminal [duplicate] - python

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

Related

The ".replace" operator isn't working 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)
Why isn't the replace() function working? [duplicate]
(1 answer)
Closed 2 years ago.
I was testing some mechanics out and ran into an issue, the following program should replace the '+' sight to ' + '. The output of this theoretically should be '20 + 20', but in reality, it's '20+20'. I have no idea why.
string = "20+20"
if string.find(" ") == -1:
string.replace("+", " + ")
print(string)
In order for this to work, you need to reassign the string variable with the result of string.replace as the replace function returns the new string.
string = "20+20"
if string.find(" ") == -1:
string = string.replace("+", " + ")
print(string)

How to remove " " from text using regex-Python 3 [duplicate]

This question already has answers here:
Remove quotes from String in Python
(8 answers)
Closed 3 years ago.
So, I have a text string that I want to remove the "" from.
Here is my text string:
string= 'Sample this is a string text with "ut" '
Here is the output I want once using a regex expression:
string= 'Sample this is a string text with ut'
Here is my overall code:
import re
string= 'Sample this is a string text with "ut" '
re.sub('" "', '', string)
And the output just show the exact text in the string without any changes. Any suggestions?
You can simply use string.replace('"','')
If you want just remove all " symbols, you can use str.replace instead:
string = string.replace('"', '')

I am trying to print some list containing '\t' and '\n' . But as python is not printing it because it is thinking it as a special character [duplicate]

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.

Remove whitespace in print function [duplicate]

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() + "*/"

Python - How to ignore white spaces in double quotes while splitting a string? [duplicate]

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!

Categories