Difference between a String and Print(String) [duplicate] - python

This question already has answers here:
What is the difference between `>>> some_object` and `>>> print some_object` in the Python interpreter?
(5 answers)
What is the difference between __str__ and __repr__?
(28 answers)
Closed 3 years ago.
>>> str4 = """This too
... is a multiline one
... built with triple double-quotes."""
>>> str4 #A
'This too\nis a multiline one\nbuilt with triple double-quotes.'
>>> print(str4) #B
This too
is a multiline one
built with triple double-quotes.
In #A and #B , we print str4 , first implicitly, then explicitly using the print function.
why the output are different.

Related

Python striping extra characters [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Why does str.lstrip strip an extra character? [duplicate]
(5 answers)
Closed 4 years ago.
I'm trying to understand what string.strip() in python is doing:
In [35]: t1 = '-MIN-North'
In [36]: t1.strip('-MIN-')
Out[36]: 'orth'
In [37]: t2 = '-MIN-north'
In [38]: t2.strip('-MIN-')
Out[38]: 'north'
Why is t1.strip('-MIN-') not equal to 'North' but t2.strip('-MIN-') equal to 'north'?
strip is taking out all the characters you provide it in the argument.
In your first example, it is stripping out the N from North because N is in -MIN-.
In the second, it is not stripping the n from north because n is not in -MIN-.

Python regex with variable {}-multiplier [duplicate]

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 6 years ago.
Say you wanted to create a pattern that matches sequences of var consecutive digits. You could do it this way:
p = re.compile(r"\d{"+str(var)+"}")
or this way:
p = re.compile(r"\d{%d}" % var)
But how would you do it using format()?
I tried both:
p = re.compile(r"\d{0}".format(var))
and:
p = re.compile(r"\d{{0}}".format(var))
but none of these worked.
You need to actually have triple { and } - two for the escaped literal braces and one for the placeholder:
In [1]: var = 6
In [2]: r"\d{{{0}}}".format(var)
Out[2]: '\\d{6}'

string strip in python [duplicate]

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
I would need to stripoff "domain\" from "domain\name" to extract name which can be any name or the word name literally
>>> s="domain\name"
>>> x=s[5:]
>>> print(x)
n
ame
>>> s="domain\bh16"
>>> x=s[5:]
>>> print(x)
h16
>>> x=s[4:]
>>> print(x)
ih16
You can convert it to a raw string and use replace as normal
s = r"domain\bh16"
print(s.replace("domain\\", '')) #bh16

Python CLI: print 'some text' vs 'some text' [duplicate]

This question already has answers here:
Understanding repr( ) function in Python
(5 answers)
Why do backslashes appear twice?
(2 answers)
Closed 7 years ago.
Why does >>> 'c\\\h' produces 'c\\\\h' via the python CLI
But >>> print 'c\\\h' produces c\\h
Python interpreter running in REPL mode prints representation (repr builtin) of result of last statement (it it exists and not a None):
>>> 5 + 6
11
For str objects representation is a string literal in a same form it is written in your code (except for the quotes that may differ), so it includes escape sequences:
>>> '\n\t1'
'\n\t1'
>>> print repr('\n\t1')
'\n\t1'
print statement (or function) on the other hand prints pretty string-conversion (str builtin) of an element, which makes all escape sequences being converted to actual characters:
>>> print '\n\t1'
<---- newline
1 <---- tab + 1

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Categories

Resources