• Register
2 votes


Hello all,

I am a new student in the Python programming language. I am learning through a textbook. I just completed the String chapter and started solving the problems of this chapter. Some of the problems are easy to solve for me. I stuck on a point and the problem says to find last occurrence of character in string python

I am surfing over the internet for a while but still didn’t find any relevant answer that I can understand easily. I think Kodlogs is easy to understand. I’ve seen some of the solutions answered by the good people.

If anybody could help me would be great for me.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


Thanks for choosing this platform. I am trying to solve your problem as easier as I can.

str = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
flag = -1
for n in range(len(str)):
    if(str[n] == char):
        flag = n
if(flag == -1):
    print("Sorry! No such character is available ")
    print("The Last Occurrence of ", char, " is Found at Position " , flag + 1)

Here if you provide the string as:

My name is Gavin

And looking for the char=n to its last occurrence. It will provide the output:

The Last Occurrence of n is Found at Position 16


13 9 6
94,240 points

Related questions

0 votes
1 answer 4 views
Problem: So if my string is "the dude is a cool dude". I'd like to find the first index of 'dude': mystring.findfirstindex('dude') # should return 4 What is the python command for this?
asked 6 days ago Mashhoodch 9.6k points
0 votes
1 answer 11 views
Problem: Hello Developers! I am new to the world of programming and I started learning Java at an academy. My teacher gave us an assingment in which we have to find the index of last occurrence of a character in a string. The program should return the ... those solutions. If someone has better solution then kindly explain or else please explain what substrings are and how I can use it. Thanks
asked Nov 29, 2020 Code Learner 6k points
0 votes
1 answer 2 views
Problem: Why I got the error : remove first and last character from string python Any ideas on what I need to do to fix this? Thanks! Can someone please help fix this, remove first and last character from string python
asked 1 day ago Mashhoodch 9.6k points
0 votes
1 answer 28 views
Problem: Hello kodlogs, Recently while attempting, the problem in the python there is a error that came can you please help sort out this problem, How can I get the first character from the first string in Python?,It seems that I could use list[0][1:] but that does not give me the first character of the string that I am looking for. mysrt = "sdf" >>> mystr[0][1:]
asked Jun 6, 2020 Gavin 15.3k points
1 vote
1 answer 20 views
Problem: For the following string I tried to convert is’s first character to a upper case. So I write a program. The string: forgodsake The program I wrote is: main_str = "forgodsake" print("The main string youput is : " + str(main_str)) key = main_str[0] + ... I am getting is not right. What is the problem in my code, why am I not getting the expected result. Can anyone sort this out?
asked Jun 10, 2020 samhaz 5k points
1 vote
1 answer 29 views
Problem: Hello Kodlogs, I am a student of the Python programming language. I am taking an online course to learn it. Every day they take classes on each topic and provide some simple problems to solve as an assignment. I had a class on String type yesterday and have got ... me a solution to make it happen for me. Please keep it as simple as possible, it will help me to understand easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 28 views
Hello kodlogs, I am a newbie in python just started learning while learning or implemented my logic for the printing a string of the element from the string but while accessing the last element I have to check every time that the index to element and it is time-consuming is there another way to get or access the element with no extra overhead?
asked Jun 8, 2020 Gavin 15.3k points
1 vote
1 answer 16 views
Problem: Hello, good people of Kodlogs, I am a new student of Python programming language. The online course I am taking they are teaching me well. Every weekend they provide me with some problems to solve. This week, I have got the problem find all substrings of a string python. I don’t have any idea how to do this! I would appreciate your effort if you pull me up. Many thanks
asked Apr 5, 2020 Gavin 15.3k points
0 votes
1 answer 20 views
Problem: Hi there! I just got an assignment in which I had to remove the first and last character from a string in Java. I did the task using substrings. I want to know what the simplest way is to do so? Also, mention other alternatives to using the substring method. Thanks!
asked Jan 8 Code Learner 6k points
0 votes
1 answer 35 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22, 2020 LizzyM 6.1k points