Python strip a string from index [duplicate] - python

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 9 years ago.
I have a list of strings in the format: 'foo7bar'. How is it possible in Python to remove the 7 along with any characters that follow?
This is similar to this question, but I need the answer for Python.

You can do this using Python's slice notation:
>>> mystr = 'foo7bar'
>>> mystr[:mystr.index('7')]
'foo'
>>>
The format for slice notation is [start:stop:step]. The index method of a string finds the position of the first occurrence of its input.
Note however that if you are dealing with something more complex (such as matching patterns), you might want to look into Regular Expressions. For this operation though, the slice notation is sufficient.

Related

Python. Variable in regular expression to find continuous (repeated) sequences of same string [duplicate]

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
I need find a repeated sequences of same my_string = 'ABCD' in string my_data = 'ABCDABCDRRRABCD' and return the longest one 'ABCDABCD'.
If I use result = ([max(i) for i in re.findall(r'((ABCD)\2+)', data)]) It's works fine, but I can't put a variable my_string so I can use it for difference strings in some loop.
To put variable inside of regular expression you can use .format
result = ([max(i) for i in re.findall(r'(({0})\2+)'.format(my_string), my_data)])

python strip function gives unexpected result [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.

Count letters in string python [duplicate]

This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi

how do i remove the first characters of a string [python] [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
thanks to answers on this site I now know that you can remove the last characters of a string by string[:-1] which was really helpfull, however I need to be able to remove the first aswell and as far as I understand this technique it is not possible. so are there other ways to remove parts of strings without replacing spesific letters?
What do you mean "it is not possible"? :)
It is perfectly possible with Explain Python's slice notation:
>>> mystr = 'abcde'
>>> mystr[1:] # Remove the first
'bcde'
>>> mystr[1:-1] # Remove the first and the last
'bcd'
>>> mystr[2:-2] # Remove the first two and the last two
'c'
>>>
string[1:]
You may need to read some documentation. :)

Colon (:) in Python list index [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
I'm new to Python. I see : used in list indices especially when it's associated with function calls.
Python 2.7 documentation suggests that lists.append translates to a[len(a):] = [x]. Why does one need to suffix len(a) with a colon?
I understand that : is used to identify keys in dictionary.
: is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end]
[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
[len(a):] is equivalent to "from length of a to end"
Watch https://youtu.be/tKTZoB2Vjuk?t=41m40s at around 40:00 he starts explaining that.
Works with tuples and strings, too.
slicing operator. http://docs.python.org/tutorial/introduction.html#strings and scroll down a bit
a[len(a):] - This gets you the length of a to the end. It selects a range. If you reverse a[:len(a)] it will get you the beginning to whatever is len(a).

Categories