Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find last occurrence of character in string Python [duplicate]

Tags:

python

How would I find the last occurrence of a character in a string?

string = "abcd}def}" string = string.find('}',last) # Want something like this 
like image 686
Apollo Avatar asked Oct 18 '14 18:10

Apollo


People also ask

How do you find the last occurrence of a character in a string in Python?

The rfind() method finds the last occurrence of the specified value. The rfind() method returns -1 if the value is not found. The rfind() method is almost the same as the rindex() method.

How do you find the last occurrence of a string in a file Python?

Here we illustrate the most reliable way to get the last occurrences of the string items by using the string rindex() method. The rindex() command returns the last existence of the substring or character if present in the Python string.

How do you find the last occurrence of a character in a string?

strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .

What is the difference between Rfind and rindex?

rindex() method is similar to rfind() method for strings. The only difference is that rfind() returns -1 if the substring is not found, whereas rindex() throws an exception.


1 Answers

You can use rfind:

>>> "abcd}def}".rfind('}') 8 
like image 154
Simeon Visser Avatar answered Sep 27 '22 21:09

Simeon Visser