• Register
2 votes
15 views

Problem:

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.
Thanks

12 7 7
15,250 points

1 Answer

1 vote

Solution:

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 ")
else:
    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

Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 19 views
19 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 Gavin 15.3k points
1 vote
1 answer 14 views
14 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 samhaz 5k points
0 votes
1 answer 23 views
23 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 Gavin 15.3k points
1 vote
1 answer 12 views
12 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 Gavin 15.3k points
1 vote
1 answer 21 views
21 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 Gavin 15.3k points