• Register
1 vote
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 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.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

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"; 
    print(string); 
    print(FirstAndLast(string)); 

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

Thanks.

13 9 6
94,240 points

Related questions

0 votes
1 answer 2 views
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 20 views
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
1 vote
1 answer 12 views
12 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, 2020 adamSw 11.3k points
0 votes
1 answer 16 views
16 views
Problem: Is there any way to remove the first and the last character in javascript like from "JohnEbra" The output should be "ohnEbr"
asked Nov 2, 2020 TeamScript 13.5k points
0 votes
1 answer 12 views
12 views asked Oct 27, 2020 xgtx 320 points
0 votes
1 answer 18 views
18 views
Problem: Hello everyone, I need to know how can I remove the last character or space from the string.
asked Oct 13, 2020 C# Learner
2 votes
1 answer 23 views
23 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, 2020 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
Problem: How can I remove first character from string? Is there any mechanism that C# provides to do so?
asked Oct 23, 2020 Code Learner 6k points
0 votes
1 answer 4 views
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
1 vote
1 answer 20 views
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