0

Suppose I have a number that I want to put in a string.

x = 0.13 

How can I format my string so that the leading zero does not appear?

print('This is my formatted number: {formating}'.format(x)) >> This is my formatted number: .13 

1 Answer 1

4

You can use the strip method:

x = 0.13 res = str(x).strip('0') print('This is my formatted number: {}'.format(res)) 
Sign up to request clarification or add additional context in comments.

2 Comments

Suppose x was a float and I wanted to truncate it to two decimal places but remove the leading zero. Is there an easy way to do that in python 3?
Yes, you can use the round function: res = str(round(x, 2)).strip('0')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.