a = "test"
b = "testing"
print a\nb
Is there a way I can use an escape sequence with a variable, or is it unnecessary?
If you are trying to print string variables by separating them with the newline character ('\n'), you can do so like this:
a = "Hello"
b = "World"
print(a+"\n"+b)
See demo here
Simply executing two separate print statements would also give a similar effect, this is because each print statement automatically inserts a newline character.
a = "Hello"
b = "World"
print(a)
print(b)
So it isn't necessary to use the newline character to escape a string while printing.
In case you'll have more to print/format in the future a maintainable solution would be,
l = [ 'Hello', 'world' ]
# Print with newlines
print('\n'.join(l))
# Print with tabs
print('\t'.join(l))
See https://docs.python.org/2/library/string.html?highlight=string%20join#string.join.
Related
I want to strip some unwanted symbols from my variable. In this case the symbols are backslashes. I am using a HEX number, and as an example I will show some short simple code down bellow. But I don't want python to convert my HEX to ASCII, how would I prevent this from happening.? I have some long shell codes for asm to work with later which are really long and removing \ by hand is a long process. I know there are different ways like using echo -e "x\x\x\x" > output etc, but my whole script will be written in python.
Thanks
>>> a = "\x31\xC0\x50\x68\x74\x76"
>>> b = a.strip("\\")
>>> print b
1�Phtv
>>> a = "\x31\x32\x33\x34\x35\x36"
>>> b = a.strip("\\")
>>> print b
123456
At the end I would like it to print my var:
>>> print b
x31x32x33x34x35x36
There are no backslashes in your variable:
>>> a = "\x31\xC0\x50\x68\x74\x76"
>>> print(a)
1ÀPhtv
Take newline for example: writing "\n" in Python will give you string with one character -- newline -- and no backslashes. See string literals docs for full syntax of these.
Now, if you really want to write string with such backslashes, you can do it with r modifier:
>>> a = r"\x31\xC0\x50\x68\x74\x76"
>>> print(a)
\x31\xC0\x50\x68\x74\x76
>>> print(a.replace('\\', ''))
x31xC0x50x68x74x76
But if you want to convert a regular string to hex-coded symbols, you can do it character by character, converting it to number ("\x31" == "1" --> 49), then to hex ("0x31"), and finally stripping the first character:
>>> a = "\x31\xC0\x50\x68\x74\x76"
>>> print(''.join([hex(ord(x))[1:] for x in a]))
'x31xc0x50x68x74x76'
There are two problems in your Code.
First the simple one:
strip() just removes one occurrence. So you should use replace("\\", ""). This will replace every backslash with "", which is the same as removing it.
The second problem is pythons behavior with backslashes:
To get your example working you need to append an 'r' in front of your string to indicate, that it is a raw string. a = r"\x31\xC0\x50\x68\x74\x76". In raw strings, a backlash doesn't escape a character but just stay a backslash.
>>> r"\x31\xC0\x50\x68\x74\x76"
'\\x31\\xC0\\x50\\x68\\x74\\x76'
supposing to have a long string to create and this string is within a method of a class, what is the best way to write the code?
def printString():
mystring = '''title\n
{{\\usepackage}}\n
text continues {param}
'''.format(param='myParameter')
return mystring
this method is well formatted but the final string has unwanted spaces:
a = printString()
print(a)
title
{\usepackage}
text continues myParameter
while this method gives the corrected results but the code can become messy if the string(s) is long:
def printString():
mystring = '''title\n
{{\\usepackage}}\n
text continues {param}
'''.format(param='myParameter')
return mystring
a = printString()
print(a)
title
{\usepackage}
text continues myParameter
some hints to have a good code quality and the results?
Try enclosing the string you want with brackets, like so:
def printString():
mystring = ('title\n'
'{{\\usepackage}}\n'
'text continues {param}').format(param='myParameter')
return mystring
This would allow you to break the string to several lines while c=having control over the whitespace.
You can use brackets to maintain tidiness of long strings inside functions.
def printString():
mystring = ("title\n"
"{{\\usepackage}}\n"
"text continues {param}"
).format(param='myParameter')
return (mystring)
print(printString())
Results in:
title
{\usepackage}
text continues myParameter
You may also wish to explicitly use the + symbol to represent string concatenation, but that changes this from a compile time operation to a runtime operation. Source
def printString():
mystring = ("title\n" +
"{{\\usepackage}}\n" +
"text continues {param}"
).format(param='myParameter')
return (mystring)
You can use re.sub to cleanup any spaces and tabs at the beginning of each lines
>>> import re
>>> def printString():
... mystring = '''title\n
... {{\\usepackage}}\n
... text continues {param}
... '''.format(param='myParameter')
...
... return re.sub(r'\n[ \t]+', '\n', mystring)
...
This gives the following o/p
>>> a = printString()
>>> print (a)
title
{\usepackage}
text continues myParameter
Below is my raw string ('\r') test in python.
import re
a = re.compile('\d')
b = re.compile('\\d')
c = re.compile(r'\d')
d = re.compile(r'\\d')
print a.search("1") # (O)
print a.search("\d")
print a.search("\1")
print b.search("1") # (O)
print b.search("\d")
print b.search("\1")
print c.search("1") # (O)
print c.search("\d")
print c.search("\1")
print d.search("1")
print d.search("\d") # (O)
print d.search("\1")
But it seems like raw string doesn't work.
For example, regular expression 'b' should catch the expression which is composed of "backslash + alphabet d", but it catches just number '1'....
And according to meaning of 'r', regular expression 'c' also should catch the string which is composed of 'backslash + alphabet d', but it didn't.
Could anyone explain this?
Thanks
Your first three strings are exactly the same.
>>> '\d' == '\\d' == r'\d'
True
Thus, when run through the regex engine, they all match only a single digit. This is true because '\d' has no interesting behavior in the way that '\n' does, so parsing the backslash as literal is the only reasonable way for the Python interpreter to respond (barring a parse error -- which I'd argue might have been a better idea, but couldn't be implemented now without breaking compatibility).
By contrast, the same is not true of \n:
>>> '\n' == '\\n'
False
>>> '\\n' == r'\n'
True
Your fourth string, r'\\d', is the same as '\\\\d'; thus, that it matches only the literal string \d should be no surprise.
>>> t = "first%s\n"
>>> t = t %("second")
>>> print t
firstsecond
Is there anyway I could retain the "\n" at the end and get "firstsecond\n" as the output?
You need to escape the slash
>>> t = "first%s\\n"
>>> t = t %("second")
>>> print t
or use raw strings:
>>> t = r"first%s\n"
>>> t = t %("second")
>>> print t
print "firstsecond\n" displays "firstsecond" and the cursor is pushed to the next new line. So you don't see any backslash followed by n. Because the display of strings implies that the special characters such as \n are interpreted.
repr() prevents the interpretation so print repr("firstsecond\n") displays firstsecond\n
Then, what do you want ? :
t being "firstsecond\n" and to display repr(t) to verify that there is the character \n in it ?
or t being "firstsecond\\n" in order that print t will display firstsecond\n ?
See:
t = "first%s\n"
print repr(t),len(t)
t = t %("second")
print repr(t),len(t)
print '-------------------'
t = "first%s\\n" # the same as r"first%s\n"
print repr(t),len(t)
t = t %("second")
print repr(t),len(t)
result
'first%s\n' 8
'firstsecond\n' 12
-------------------
'first%s\\n' 9
'firstsecond\\n' 13
But don't make misinterpretation: when there is a display like that:
'first%s\\n' ,
the two backslashes \\ mean a value of ONE backslash. The two \\ appear only on the screen to express the value of a backslash in an escaped manner. Otherwise, it couldn't be possible to differentiate the two characters consisting of \ followed by n and the unique character \n
Depending on what do you need exactly, you may also check repr().
>>> s = "firstsecond\n"
>>> print repr(s)
'firstsecond\n'
I'm trying to remove the last 3 characters from a string in Python, I don't know what these characters are so I can't use rstrip, I also need to remove any white space and convert to upper-case.
An example would be:
foo = "Bs12 3ab"
foo.replace(" ", "").rstrip(foo[-3:]).upper()
This works and gives me "BS12" which is what I want, however if the last 4th & 3rd characters are the same I lose both, e.g. if foo = "BS11 1AA" I just get "BS".
Examples of foo could be:
BS1 1AB
bs11ab
BS111ab
The string could be 6 or 7 characters and I need to drop the last 3 (assuming no white space).
Removing any and all whitespace:
foo = ''.join(foo.split())
Removing last three characters:
foo = foo[:-3]
Converting to capital letters:
foo = foo.upper()
All of that code in one line:
foo = ''.join(foo.split())[:-3].upper()
It doesn't work as you expect because strip is character based. You need to do this instead:
foo = foo.replace(' ', '')[:-3].upper()
>>> foo = "Bs12 3ab"
>>> foo[:-3]
'Bs12 '
>>> foo[:-3].strip()
'Bs12'
>>> foo[:-3].strip().replace(" ","")
'Bs12'
>>> foo[:-3].strip().replace(" ","").upper()
'BS12'
You might have misunderstood rstrip slightly, it strips not a string but any character in the string you specify.
Like this:
>>> text = "xxxxcbaabc"
>>> text.rstrip("abc")
'xxxx'
So instead, just use
text = text[:-3]
(after replacing whitespace with nothing)
>>> foo = 'BS1 1AB'
>>> foo.replace(" ", "").rstrip()[:-3].upper()
'BS1'
I try to avoid regular expressions, but this appears to work:
string = re.sub("\s","",(string.lower()))[:-3]
split
slice
concentrate
This is a good workout for beginners and it's easy to achieve.
Another advanced method is a function like this:
def trim(s):
return trim(s[slice])
And for this question, you just want to remove the last characters, so you can write like this:
def trim(s):
return s[ : -3]
I think you are over to care about what those three characters are, so you lost. You just want to remove last three, nevertheless who they are!
If you want to remove some specific characters, you can add some if judgements:
def trim(s):
if [conditions]: ### for some cases, I recommend using isinstance().
return trim(s[slice])
What's wrong with this?
foo.replace(" ", "")[:-3].upper()
Aren't you performing the operations in the wrong order? You requirement seems to be foo[:-3].replace(" ", "").upper()
It some what depends on your definition of whitespace. I would generally call whitespace to be spaces, tabs, line breaks and carriage returns. If this is your definition you want to use a regex with \s to replace all whitespace charactors:
import re
def myCleaner(foo):
print 'dirty: ', foo
foo = re.sub(r'\s', '', foo)
foo = foo[:-3]
foo = foo.upper()
print 'clean:', foo
print
myCleaner("BS1 1AB")
myCleaner("bs11ab")
myCleaner("BS111ab")