Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my code, I have a string that contains " and ' in between how to deal with it?
And it is randomly generated every time I generate a string at different positions and gives me an error when I try to print due to " or ' it marks as the end of some strings and the post characters of that string generate an error.
Edit 1:
An example for such type of string is:
85)p$85"5p9p1=p%#9>7p'81$#1
Thanks in advance
You need to escape it using the \. If your string starts with double quote, then escape \' and escape \" if starts with single one.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.
res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
How can we reduce a string like haaaaaaapppppyyyyyy to haappyy
Such that repetition is allowed to a maximum of twice in a row for a character in a string?
including any character ( special characters also )
converting --------------------- to --
We can use a regex replacement:
inp = "haaaaaaapppppyyyyyy"
output = re.sub(r'(\w)\1{2,}', r'\1\1', inp)
print(output) # haappyy
The above logic matches any one character which is followed by itself two or more times. It then replaces with just two of the character.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Suppose, I want to define a two-line (or a multi-line) string.
I can do this in two ways:
Using escape sequence for the newline character.\n
Ex: "This is the first sentence. \n This is the second sentence."
Using triple-quoted strings.
Ex: """ This is the first sentence.
This is the second sentence."""
Which is the more efficient or conventional ? Why ?
I'm tempted to say it doesn't matter since each one still scans inside for escaped characters while parsing the text.
>>> print "a\n\tb"
a
b
>>> print """a\n\tb"""
a
b
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I remove the hex value from a string in Python 2.7? Here is the string,
\xffDSI\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x00\x08\x00\x00\x00\x00\x00\x00\x00#\x00\x00\x00#\x00\x00\x00\x01\x04\xb3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x00\x04\x00\x02\x00\x01\x00\x01\x00\x02\x00\x06\x00\x06"\x00\x00\x00\x00c\x01,\x00\x00\x06&\x00\x00\x00\x01\x01,\x00\x00\x06\'\x00\x00\x11\x98\x00\x19\x00\x00\x00(\x00\x00\x00\x00\x00\x0011_w\x00\x00\x00\x00\x00\x006\x00A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01,\x00\x00\x17\xbf\x00\x00\x11\x98\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
#b4
,sudd5ly1
!
toddl]
0
able
to
use
a
comput]
to
play
games4
\x00$\x00\x00\x018\x00\x00\x00\x00\x00\x01\x00\x19\x00\x02\x00\x03\x00\x04\x00\x05\x00\x1d\x00\x1f\x00\x06\x00\x07\x00\x08\x00\t\x00'],
['\x00\x0b\x00\x0c\x00\r\x00\x1b\x00\x0e\x00\x0f\x00\x10\x00\x13\x00\x11\x00\x14\x00\x1c\x00\x18\x00
I want to display only #b4 to games4. All the hex values should be removed. Thank you.
What I am trying to do is to read in the file type *.dxb, which display braille font. I was able to read the file but the output showed me all those \xffDSI\x00... and then #b4 ,sudd5ly1
The #b4 ,sudd5ly1 is only the part that I want the output to show so that I can do a comparison with other file.
Thank you again.
You can use something like this:
import string
s = '\xffDSI....'
cleaned = ''.join(c for c in s if c in string.printable)
This uses printable as the definition of "not a hex value", though it does include \x0b and \x0c (both printable whitespace characters).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I replace all the " with the raw string \" in a string such as "Hello", said he. in Python?
s = '"Hello", said he.'
print s.replace('"', r'\"')
# output
\"Hello\", said he.
It helps to use the r'' notation to indicate that the string should be raw and not interpreted. Helps with backslashes.
Use replace().
>>> print '"Hello"'.replace('"', '\\"')
\"Hello\"