What are the various ways to manipulate a string in Python?

0 votes
I have a string in Python. I have to cut the last 3 alphabets of that string. So how do I do this?
4 days ago in Python by Arvind
• 2,520 points
10 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

There are 2 ways to do this. Suppose you have a string a = "maddy". Now refer to the code below.

1. The first way is simply by using string slicing. For example,

>>> a="maddy"
>>> a[:-3]
'ma'

a[:-3] will simply remove the last 3 characters of the string.

2. The second way to do it is by suing rstrip(). For example, suppose that we have the same string a = "maddy"

>>> a=a.rstrip(a[-3:])
>>> a
'ma'

The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed).

answered 4 days ago by Neel
• 2,450 points

Related Questions In Python

0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
69 views
–1 vote
2 answers

How to find the size of a string in Python?

following way to find length of string  x ...READ MORE

answered Mar 29 in Python by rajesh
• 1,210 points
72 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,670 points

reshown Aug 24, 2018 by Priyaj 58 views
0 votes
1 answer

What is the process to kill a particular thread in python?

A multiprocessing.Process can p.terminate() In the cases where I want to ...READ MORE

answered Feb 4 in Python by charlie_brown
• 7,710 points
44 views
0 votes
1 answer

What are the best IDE/platform to write code in Python

Just as any other programming language, even ...READ MORE

answered May 28 in Python by Harsh
• 260 points
29 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 168 views
+4 votes
6 answers