Write a Python Program to Parse a String to a Float or Int

Python Program to Parse a String to a Float or Int

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a Python Program to Parse a String to a Float or Int

PROGRAM: Python Program to Parse a String to a Float or Int

/* Python Program to Parse a String to a Float or Int */

 1: Parse string into integer

balance_str = “1500”
balance_int = int(balance_str)

# print the type
print(type(balance_int))

# print the value
print(balance_int)

Output

<class 'int'>
1500

2: Parse string into float

balance_str = “1500.4”
balance_float = float(balance_str)

# print the type
print(type(balance_float))

# print the value
print(balance_float)

Output

<class 'float'>
1500.4

3: A string float numeral into integer

balance_str = “1500.34”
balance_int = int(float(balance_str))

# print the type
print(type(balance_int))

# print the value
print(balance_int)

Output

<class 'int'>
1500

Leave a Reply

Your email address will not be published. Required fields are marked *