I am using Sublime Text version 3.0 and my input is:
little_known_person = "' Christopher '"
print(little_known_person.strip())
but my output is ' Christopher ' instead of 'Christopher'. The output is the same if I try the rstrip() and lstrip() methods.
You need to strip leading/trailing whitespace and single quotes. The default, with no argument, is to strip only whitespace.
little_known_person.strip(" '")
The argument is an arbitrary iterable (str, list, tuple, etc) containing characters that should be stripped. The order doesn't matter.
You need to strip whitepaces and single quotes and then re-add the quotes:
little_known_person = "'" + little_known_person.strip(" '") + "'"
Please try if the following approach solves your issue:
>>> little_known_person = "' Christopher '"
>>> little_known_person.replace("'", "").strip()
'Christopher'
Related
Need a solution for bellow code.
variable = ' "value" '
How to get variable = 'value'
Thanks
try:
variable.replace("\"","").strip()
replace replaces the double quotes with nothing (removes it) and strip() removes the trailing and leading spaces
You can try this:
s = ' "value" '
s = s[2:-2]
print(s)
Output:
value
I have a CSV file, where each comma delimited field is enclosed in " - eg. "fred", "bert", "blah". I am trying to use the replace function but can't seem to have it recognize the " character. example, if the record is in a string called buffer:
buffer.replace('\"','')
Add space between double quotes
p = '"fred", "bert", "blah"'
p.replace('\"'," ")
' fred , bert , blah '
Why do you escape the double quotes if it's inside single quotes ?
Try the following :
a = '"my string"'
a = a.replace('"',' ')
print(a)
#=> ' my string '
You are not replacing it with space firstly, but with empty string
Try using buffer.strip("\"")
I'm so confused... why/how is a different from b?! Why don't they print the same thing?
>>> a = '"'
>>> a
'"'
>>> b = "'"
>>> b
"'"
The strings are not presented differently. Their presentation is just adjusted to avoid having to quote the contained quote. Both ' and " are legal string literal delimiters.
Note that the contents of the string are very different. " is not the same string as '; a == b is (patently) False.
Python would have to use a \ backslash for the " or ' character otherwise. If you use both characters in a string, then python is forced to use quoting:
>>> '\'"'
'\'"'
>>> """Tripple quoted means you can use both without escaping them: "'"""
'Tripple quoted means you can use both without escaping them: "\''
As you can see, the string representation used by Python still uses single quotes and a backslash to represent that last string.
Recently I made the following observation:
>>> x= "\'"
>>> x
"'"
>>> y="'"
>>> y
"'"
>>> print x
'
>>> print y
'
Can anyone please explain why is it so. I am using python 2.7.x. I know well about escape sequences.
I want to do the following:
I have a string with single quotes in it and I have to enter it in a database so I need to replace the instance of single quote(') with a backslash followed by a single quote(\'). How can I achieve this.
Inside a pair of "", you don't need to escape the ' character. You can, of course, but as you've seen it's unnecessary and has no effect whatsoever.
It'd be necessary to escape if you were to write a ' inside a pair of '' or a " inside a pair of "":
x = '\''
y = "\""
EDIT :
Regarding the last part in the question, added after the edit:
I have a string with single quotes in it and I have to enter it in a database so I need to replace the instance of single quote(') with a backslash followed by a single quote(\'). How can I achieve this
Any of the following will work, notice the use of raw strings for avoiding the need to escape special characters:
v = "\\'"
w = '\\\''
x = r'\''
y = r"\'"
print v, w, x, y
> \' \' \' \'
When you run something through popen in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?
You can simply do
s = s.replace('\r\n', '\n')
to replace all occurrences of CRNL with just NL, which seems to be what you want.
buffer = "<text from your subprocess here>\r\n"
no_cr = buffer.replace("\r\n", "\n")
If they are at the end of the string(s), I would suggest to use:
buffer = "<text from your subprocess here>\r\n"
no_cr = buffer.rstrip("\r\n")
You can also use rstrip() without parameters which will remove whitespace as well.
replace('\r\n','\n') should work, but sometimes it just does not. How strange. Instead you can use this:
lines = buffer.split('\r')
cleanbuffer = ''
for line in lines: cleanbuffer = cleanbuffer + line
Actually, you can simply do the following:
s = s.strip()
This will remove any extraneous whitespace, including CR and LFs, leading or trailing the string.
s = s.rstrip()
Does the same, but only trailing the string.
That is:
s = ' Now is the time for all good... \t\n\r "
s = s.strip()
s now contains 'Now is the time for all good...'
s = s.rstrip()
s now contains ' Now is the time for all good...'
See http://docs.python.org/library/stdtypes.html for more.
You can do s = s.replace('\r', '') too.