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-.
Related
This question already has answers here:
Python truncate a long string
(22 answers)
Closed 5 months ago.
How can I remove the final characters from a string in python until I reach a certain amount of characters?
How can I turn: abcdefghijklmnopqrstuvwxyz => abcdefghijklmnopqr; using python?
Try string slicing.
You can use negative indexing:
'abcdefghijklmnopqrstuvwxyz'[:-8]
or positive:
'abcdefghijklmnopqrstuvwxyz'[:18]
Pick your poison
You can treat the string as an array of symbols
c_1 = "abcdefghijklmnopqrstuvwxyz"
l = 18
c_2 = c_2[:l]
This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Changing one character in a string
(15 answers)
Closed 4 years ago.
So I have this string: "My Wurds" and I'd like to replace the u at index 4 with o. what is the proper Python way to do that?
You could build a new string using slices:
s = "My Wurds"
t = s[:4] + "o" + s[5:]
If you know the index of u where you need to replace with o, a pretty straight approach would be:
s[:4] + s[4:].replace('u', 'o', 1)
replace('u', 'o', 1) will ensure replace of only first occurance.
This question already has answers here:
Remove characters from beginning and end or only end of line
(5 answers)
Closed 4 years ago.
So, I have the following string "........my.python.string" and I want to remove all the "." until it gets to the first alphanumeric character, is there a way to achieve this other than converting the string to a list and work it from there?
You can use re.sub:
import re
s = "........my.python.string"
new_s = re.sub('^\.+', '', s)
print(new_s)
Output:
my.python.string
This question already has answers here:
Split string every nth character?
(19 answers)
Closed 5 years ago.
I have a string
ddffjh3gs
I want to convert it into a list
["ddf", "fjh", "3gs"]
In groups of 3 characters as seen above.
What is the best way to do this in python 2.7?
Using list comprehension with string slice:
>>> s = 'ddffjh3gs'
>>> [s[i:i+3] for i in range(0, len(s), 3)]
['ddf', 'fjh', '3gs']
This question already has answers here:
Find index of last occurrence of a substring in a string
(12 answers)
Closed 8 years ago.
How would I find the last occurrence of a character in a string?
string = "abcd}def}"
string = string.find('}',last) # Want something like this
You can use rfind:
>>> "abcd}def}".rfind('}')
8