• Register
1 vote


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 a few problems to solve. I’ve solved them except one. The last problem is saying, Remove first and last character from string python.

I tried in different ways but I failed to succeed.  Could any of you please give me a solution to make it happen for me. Please keep it as simple as possible, it will help me to understand easily.


12 7 7
15,250 points

1 Answer

1 vote
Best answer


You can easily do it by using the .split() function. Have a look at below code snippet. I used a for-loop iteration to check my string and remove characters from my string.

def FirstAndLast(string) :   
    myStr = string.split(); 
    res = ""; 
    for n in myStr : 
        res += n[1:len(n) - 1] + " "; 
    return res; 
if __name__ == "__main__" :    
    string = "My Name Is Gavin"; 

Reply to this thread if you have any confusion about it. Leave a kudos if you solved the problem.


13 9 6
94,240 points

Related questions

1 vote
1 answer 9 views
Problem: Many of my friends told me that Python is a better programming language for beginners. It is easy to pick up. I was convinced by their words and started learning Python. As days are passing I am discovering new things and it keeps attracting me. Well, today I was ... better way to do that job! I completely understand that I didn’t do it in an appropriate way. What do you say, guys?
asked May 31 adamSw 11.3k points
0 votes
1 answer 4 views
Problem: Hello everyone, I need to know how can I remove the last character or space from the string.
asked Oct 13 C# Learner
2 votes
1 answer 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 ... understand. I’ve seen some of the solutions answered by the good people. If anybody could help me would be great for me. Thanks
asked Mar 29 Gavin 15.3k points
1 vote
1 answer 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 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