Python: .strip() method is not working as expected [duplicate] - python

This question already has answers here:
How do the .strip/.rstrip/.lstrip string methods work in Python?
(4 answers)
Closed 2 years ago.
I have two strings:
my_str_1 = '200327_elb_72_ch_1429.csv'
my_str_2 = '200327_elb_10_ch_1429.csv'
When I call .strip() method on both of them I get results like this:
>>> print(my_str_1.strip('200327_elb_'))
'ch_1429.csv'
>>> print(my_str_2.strip('200327_elb_'))
'10_ch_1429.csv'
I expected result of print(my_str_1.strip('200327_elb_')) to be '72_ch_1429.csv'. Why isn't it that case? Why these two result aren't consistent? What am I missing?

From the docs:
[...] The chars argument is a string specifying the set of characters to be removed. [...] The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped [...]
This method removes all specified characters that appear at the left or right end of the original string, till on character is reached that is not specified; it does not just remove leading/trailing substrings, it takes each character individually.
Clarifying example (from Jon Clements comment); note that the characters a from the middle are NOT removed:
>>> 'aa3aa3aa'.strip('a')
'3aa3'
>>> 'a4a3aa3a5a'.strip('a54')
'3aa3'

Related

Is there any bug in Python strip() function? [duplicate]

This question already has answers here:
How to use text strip() function?
(2 answers)
Closed 5 years ago.
Creating two strings:
s1 = "sha1:abcd"
s2 = "sha1:wxyz"
Applying .strip() function on both strings:
s1.strip("sha1:")
>>> 'bcd'
s2.strip("sha1:")
>>> 'wxyz'
I expected the following output:
s1.strip("sha1:")
>>> 'abcd'
s2.strip("sha1:")
>>> 'wxyz'
I am aware that strip() function is deprecated. I am just curious to know the issue. I went through official docs, but found no special mentions about ":a" or anything like that.
And also I am aware of other alternatives, we can use split("sha1:") or strip("sha1") followed by strip(":"), gives the desired output.
there
strip(...)
S.strip([chars]) -> str
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
note characters in chars
Explained in detail in the documentation.
Here is a counter example showing the actual intention of strip:
s1 = "sha1:abcds"
s2 = "sha1:wxyzs"
print(s1.strip("sha1:"))
print(s2.strip("sha1:"))
Output:
bcd
wxyz
strip() removed chars supplied in its parameter, whether they are found at the start or end of the target.
It will strip all characters i.e. s, h, a , 1 and : at the beginning and ending of the string.

Capture repeated characters and split using Python [duplicate]

This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 3 years ago.
I need to split a string by using repeated characters.
For example:
My string is "howhowhow"
I need output as 'how,how,how'.
I cant use 'how' directly in my reg exp. because my input varies. I should check the string whether it is repeating the character and need to split that characters.
import re
string = "howhowhow"
print(','.join(re.findall(re.search(r"(.+?)\1", string).group(1), string)))
OUTPUT
howhowhow -> how,how,how
howhowhowhow -> how,how,how,how
testhowhowhow -> how,how,how # not clearly defined by OP
The pattern is non-greedy so that howhowhowhow doesn't map to howhow,howhow which is also legitimate. Remove the ? if you prefer the longest match.
lengthofRepeatedChar = 3
str1 = 'howhowhow'
HowmanyTimesRepeated = int(len(str1)/lengthofRepeatedChar)
((str1[:lengthofRepeatedChar]+',')*HowmanyTimesRepeated)[:-1]
'how,how,how'
Works When u know the length of repeated characters

How to join() words from a string? [duplicate]

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 6 years ago.
I want to transform the string 'one two three' into one_two_three.
I've tried "_".join('one two three'), but that gives me o_n_e_ _t_w_o_ _t_h_r_e_e_...
how do I insert the "_" only at spaces between words in a string?
You can use string's replace method:
'one two three'.replace(' ', '_')
# 'one_two_three'
str.join method takes an iterable as an argument and concatenate the strings in the iterable, string by itself is an iterable so you will separate each character by the _ you specified, if you directly call _.join(some string).
You can also split/join:
'_'.join('one two three'.split())
And if you want to use join only , so you can do like thistest="test string".split()
"_".join(test)
This will give you output as "test_string".

adding string ending "\" to all elements of a list of strings [duplicate]

This question already has answers here:
What is the difference between __str__ and __repr__?
(28 answers)
Closed 6 years ago.
I have a list of strings: ['John','William','Ken','Rogers']. I need to prepend "Corp\" to each element in the list so that the final list looks like this:
['Corp\John','Corp\William','Corp\Ken','Corp\Rogers']
I tried the following:
s=['John','William','Ken','Rogers']
users=['Corp\\' + m for m in s]
print(users)
The output gives me
['Corp\\John','Corp\\William','Corp\\Ken','Corp\\Rogers']
If I try users=['Corp\' + m for m in s] I get an obvious error:
"StringError EOL while scanning string literal"
I would need each element in the exact form 'Corp\name', as this needs to be used in a for loop to validate users who are eligible to login.
This may be a problem with how you're 'outputting' the list. Using the REPL:
>>> lsa = ["Corp\{}".format(item) for item in ls]
>>> print(lsa)
['Corp\\Jenna', 'Corp\\Wilma', 'Corp\\Katie', 'Corp\\Rebecca']
>>> for i in lsa:
... print(i)
...
Corp\Jenna
Corp\Wilma
Corp\Katie
Corp\Rebecca
As you can see, in the first print, that prints the full list, we see two slashes. This is because Python is saying that the second slash is escaped. In the second print, inside a for loop, we see that there is only one slash, because we are printing each item individually and the escape string is applied, yielding only a single slash.

Removing a prefix from a string [duplicate]

This question already has answers here:
Remove a prefix from a string [duplicate]
(6 answers)
Closed 6 months ago.
Trying to strip the "0b1" from the left end of a binary number.
The following code results in stripping all of binary object. (not good)
>>> bbn = '0b1000101110100010111010001' #converted bin(2**24+**2^24/11)
>>> aan=bbn.lstrip("0b1") #Try stripping all left-end junk at once.
>>> print aan #oops all gone.
''
So I did the .lstrip() in two steps:
>>> bbn = '0b1000101110100010111010001' # Same fraction expqansion
>>> aan=bbn.lstrip("0b")# Had done this before.
>>> print aan #Extra "1" still there.
'1000101110100010111010001'
>>> aan=aan.lstrip("1")# If at first you don't succeed...
>>> print aan #YES!
'000101110100010111010001'
What's the deal?
Thanks again for solving this in one simple step. (see my previous question)
The strip family treat the arg as a set of characters to be removed. The default set is "all whitespace characters".
You want:
if strg.startswith("0b1"):
strg = strg[3:]
No. Stripping removes all characters in the sequence passed, not just the literal sequence. Slice the string if you want to remove a fixed length.
In Python 3.9 you can use bbn.removeprefix('0b1').
(Actually this question has been mentioned as part of the rationale in PEP 616.)
This is the way lstrip works. It removes any of the characters in the parameter, not necessarily the string as a whole. In the first example, since the input consisted of only those characters, nothing was left.
Lstrip is removing any of the characters in the string. So, as well as the initial 0b1, it is removing all zeros and all ones. Hence it is all gone!
#Harryooo: lstrip only takes the characters off the left hand end. So, because there's only one 1 before the first 0, it removes that. If the number started 0b11100101..., calling a.strip('0b').strip('1') would remove the first three ones, so you'd be left with 00101.
>>> i = 0b1000101110100010111010001
>>> print(bin(i))
'0b1000101110100010111010001'
>>> print(format(i, '#b'))
'0b1000101110100010111010001'
>>> print(format(i, 'b'))
'1000101110100010111010001'
See Example in python tutor:
From the standard doucmentation (See standard documentation for function bin()):
bin(x)
Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer. Some examples:
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
If prefix “0b” is desired or not, you can use either of the following ways.
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
See also format() for more information.

Categories

Resources