Specify Python 3 to not meddle with new line? - python-3.x

I have the following code in Python 3:
st = "a" + '\r\n\r\n\r\n' + "b"
print( st )
The output is the following:
I do not want Python to add a 'CR' for me - I need to be in control. Is there anything I can do about it?

The built-in method repr() will return the string without the newline formatting.
https://docs.python.org/3/library/functions.html#repr
>>> st = "a" + '\r\n\r\n\r\n' + "b"
>>> print(repr(st))
'a\r\n\r\n\r\nb'
Alternatively, you can use a raw string like as demonstrated below.
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
>>> print("a" + r'\r\n\r\n' + "b")
a\r\n\r\nb

Except the last one, all others are there just because you are telling python to write them (\r for LF and \n for CR). If you refer to the CR LF then you can use:
print(st,end="\n")

Related

my code is right but Lua replace doesn't working (:gsub)

Hello I am trying to replace a specific text to "" and my code doesn't working. I just don't know why my code not working
b = 'Just testing.<script>window.location.replace("http://google.com");</script>'
print(b)
b = b:gsub('<script>window.location.replace("http://google.com");</script>', "")
print(b)
out 1: Just testing.window.location.replace("http://google.com");
out 2: Just testing.window.location.replace("http://google.com");
I tried b = string.gsub(b,'<script>window.location.replace("http://google.com");</script>',"") too but its doesn't worked either
I am working in FiveM
You need to escape ( and ), in a lua pattern they are recognized as special character. you can escape them using %
b = 'Just testing.<script>window.location.replace("http://google.com");</script>'
print(b)
b = b:gsub('<script>window.location.replace%("http://google.com"%);</script>', "")
print(b)
For more infomration on lua patter you can look at these resources:
Understanding Lua Patterns
20.1 – Pattern-Matching Functions
20.2 – Patterns

Inserting values into strings in Python

I am trying to iterate over some integer values and insert them into an string which has to be in a weird format to work. The exact output (including the outer quotes) I need if the value was 64015 would be:
"param={\"zip\":\"64015\"}"
I have tried f string formatting but couldn't get it to work. It has problem with the backslashes and when I escaped them the output was not exactly like above string
Hopefully, I made myself clear enough.
You would have to escape the backslash and the double quotes seperately like this:
string = '"param={\\\"zip\\\":\\\"' + str(64015) + '\\\"}"'
The result of this is:
"param={\"zip\":\"64015\"}"
You can use alternate ways to delimit the outer string ('...', '''...''', """...""") or use str.format() or old style %-formatting syntax to get there (see f-style workaround at the end):
s = s = 'param={"zip":"' + str(64015) + '"}'
print(s)
s = '''param={"zip":"''' + str(64015) +'''"}'''
print(s)
s = """param={"zip":"64015"}""" # not suited for variable replacement
print(s)
s = 'param={{"zip":"{0}"}}'.format(64015)
print(s)
s = 'param={"zip":"%s"}' % 64015
print(s)
Output:
param={"zip":"64015"}
param={"zip":"64015"}
param={"zip":"64015"}
param={"zip":"64015"}
If you need any "\" in there simply drop a \\ in:
s = '"param={\\"zip\\":\\"' + str(64015) + '\\"}"'
print(s)
s = '''"param={\\"zip\\":\\"''' + str(64015) +'''\\"}"'''
print(s)
s = '"param={{\\"zip\\":\\"{0}\\"}}"'.format(64015)
print(s)
s = '"param={\\"zip\\":\\"%s\\"}"' % 64015
print(s)
Output:
"param={\"zip\":\"64015\"}"
"param={\"zip\":\"64015\"}"
"param={\"zip\":\"64015\"}"
"param={\"zip\":\"64015\"}"
The f-string workaround variant would look like so:
a = '\\"'
num = 64015
s = f'"param={{{a}zip{a}:{a}{num}{a}}}"'
and if printed also yields :
"param={\"zip\":\"64015\"}"
More on the topic can be found here: 'Custom string formatting' on python.org
I played around a bit with f-strings and .format() but ultimately got this to work:
foo = 90210
bar = '"param={\\"zip\\":\\"%s\\"}"' % (foo)
print(bar)
giving:
"param={\"zip\":\"90210\"}"
Hopefully someone can give you an f-string alternative. I kept running into unallowed "\" in my f-string attempts.
Is it only this?
a = "param={\"zip\":\"64015\"}"
b = a.split('=')
c = eval(b[1])
print(c)
print(c['zip'])
Result:
{'zip': '64015'}
64015
Please note that evaluating (eval()) strings from unknown source may
be dangerous! It may run the code that you are not expecting.

Python inserting characters in strings next to a specefic letters

I have a problem with adding p + vowel after a given vowel in a string using Python.
For example:
If I write welcome, the program would print wepelcopomepe.
You can use regex. Here is an example:
import re
s = 'welcome'
new_s = re.sub('([aeiou])', '\g<1>p\g<1>', s)
print(new_s)
> wepelcopomepe

String search keep returning empty in python

I have kept trying to get my code to work but I keep getting blank output and I am not allowed to import anything e.g. RE:
choc1 =' outf.write("/# " + str(number) + " #/ " + line) #lots of cake(#) lovers here'
EC = choc1
ECout = EC
out = ""
for x in ECout :
if x!="#[a-z,A-Z]":
x = x.replace(x,"")
out += x
if out== '#lots of cake(#) lovers here':
print("well done Lucy")
else:
print(out)
I must be really stupid as this should be simple - I need to return '#lots of cake(#) lovers here' but I'm stuck on this assignment.
Any help would be greatly appreciated.
Thanks in advance - Jemma
Looking at your for loop, you are examining each character in a string individually, and comparing this to a string that is several characters long. Is this what you intended? Consider:
>>> a = "abc"
>>> for x in a:
... print(x)
...
a
b
c
Perhaps you meant to take each character, and compare that character to each character separately in the other string with a slightly different statement?

Python 3 unicode ZWJ error with String replace

I need to replace ANSII characters with UNICODE (Sinhala). I use lists with a loop to do that as follows,
for i in range (len(charansi)):
for j in range (len(charUni)):
s = charansi[i] + ansimod[j]
v = charUni[i] + modUni[j]
textSource = textSource.replace(s, v)
if we use n + uu as ANSII input, it should give නූ as Unicode out put. But instead of that, it gives න ූ
to clarify more,
charansi = n
ansimod = uu
charUni = න
modUni = ූ
this න and ූ must join without spaces. I think ZWJ (\u200D) plays a role here. so i tried
v = u"\u200D".join((consonantsUni[i], vowelModifiersUni[j]))
gives same result.
How do I fix this issue?
Your question is a bit confusing, but this simply works:
#coding:utf8
charansi = 'n'
ansimod = 'uu'
charUni = 'න'
modUni = 'ූ'
v = s.replace(charansi+ansimod,charUni+modUni)
print(v)
Output:
නූ
Create a working example of the problem if this isn't what you want.
You could also use the following to make the characters more clear. At least on my browser, the modifier didn't display very well.
charUni = '\N{SINHALA LETTER DANTAJA NAYANNA}'
modUni = '\N{SINHALA VOWEL SIGN DIGA PAA-PILLA}'

Resources