How remove minus from string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How remove minus from string?

How to remove minus from string E.g " -50" to "50" I have output -50 but i want to show 50 so what method i can use.

23rd Mar 2022, 5:32 AM
Maulik Panchal
Maulik Panchal - avatar
7 Answers
+ 4
x.lstrip('-')
24th Mar 2022, 2:24 AM
Vaibhav
Vaibhav - avatar
+ 4
s = '-50' print(abs(int(s))) n = -50 print(abs(n)
24th Mar 2022, 3:15 AM
CodeStory
CodeStory - avatar
+ 3
your_string = "-50" print(your_string[1:])
23rd Mar 2022, 5:51 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
# Hi! One way is to use replace: >>> s = "-40" >>> p = s.replace('-', '') >>> print(p) 40
23rd Mar 2022, 7:00 AM
Per Bratthammar
Per Bratthammar - avatar
+ 3
MONSTER MIKE , this issue may be also solved in a different way. ▪︎where is this string "-50" coming from? ▪︎do you get a string that comes from an input or from a file? ▪︎or is it created by a calculation and then converted to string? thanks!
23rd Mar 2022, 11:09 AM
Lothar
Lothar - avatar
+ 2
Hi Mike! There are many ways to remove minus from a string. I think using slicing method is a better option . num = "-50" print(num[1:]) It returns all characters from 2nd character.
23rd Mar 2022, 5:51 AM
Python Learner
Python Learner - avatar
+ 1
Lothar, It created by calculation and i find solution but thank you for your time
23rd Mar 2022, 11:15 AM
Maulik Panchal
Maulik Panchal - avatar