# How to remove decimals from a string in Python [4 Methods]

Are you interested in learning to remove decimals from a string in Python? If so, then you have come to the right place.

In this Python article, I will cover several different methods to remove decimals from a string in Python with some practical examples.

Keep reading this article to learn these different methods to remove decimals from a string in Python.

• Using re.sub() method
• Using split() and join() method
• Using string conversion
• Using math.floor() method

## Python program to remove decimals from a string using the re.sub() method

In Python, the sub() is a function in the built-in re-module that handles regular expression. The sub() function in Python returns a string after replacing the matched pattern in a string with a replacement.

We can use regular expressions to remove decimal parts in Python from strings by matching the pattern of numbers with decimals and extracting the integer part.

This is the source code of Python to remove decimals from a string using the re.sub() methods.

``````import re

def remove_decimal(string_with_decimal):
return re.sub(r'\.\d+', '', string_with_decimal)

string_with_decimal = "412.66"
result = remove_decimal(string_with_decimal)
print(result)``````

This Python code line is used to find all occurrences of a decimal followed by one or more digits and replace them with an empty string, then remove the decimal part from the string.

`` return re.sub(r'\.\d+', '', string_with_decimal)``

Output:

``412``

Refer to the below image to see the output of the Python program to remove decimals from a string in Python using the re.sub() method.

## How to remove decimals from a string in Python using split() and join() method

This is another approach to remove decimals from a string using the split() and join() methods in Python.

READ:  How to extract all values from a dictionary in Python

The split() function in Python is used to split a string into a list, whereas the join() function takes all items in an iterable and joins them into one string.

Here, is the complete Python code to remove decimals from a string using the split() and join() method in Python.

``````def remove_decimal_num(string_decimal):
return ''.join(string_decimal.split('.'))

string_decimal = "672.56"
output_num = remove_decimal_num(string_decimal)
print(output_num)``````

We used this to split the input string at each occurrence of the decimal point and create a list of substrings.

Then, the join() function in Python keeps the substrings back together into a single string, and an empty string is used as a separator to join the substring together and remove the decimal point.

`` return ''.join(string_decimal.split('.'))``

Output:

``67256``

The screenshot below shows the Python program’s output to remove decimals from a string using the split() and join() methods.

## Remove decimals from a string in Python using the math.floor() method

In Python, the math.floor() method rounds a number down to the nearest integer and returns the result.

By using this math.floor() method, we can remove decimals from a string in Python.

This is the Python code to remove decimals from a string using math.floor() method.

``````import math

def remove_number(string_with_decimal):
return str(math.floor(float(string_with_decimal)))

string_with_decimal = "37.56"
output_value = remove_number(string_with_decimal)
print(output_value) ``````

Here, it rounds down the given number to the nearest integer, and then it converts the integer back to a string.

`` return str(math.floor(float(string_with_decimal)))``

Output:

``37``

You can refer to the image below to see the output of the Python program to remove the decimal from a string using math.floor() method.

## How to remove decimals from a string in Python using a custom function with string conversion

This is another approach to removing decimals from a string using a custom function with string conversion in Python.

This is the Python full source code to remove decimals from a string using a custom function with string conversion.

``````def remove_decimal(string_with_decimal):
return str(int(float(string_with_decimal)))

string_with_decimal = "3711.56"
result = remove_decimal(string_with_decimal)
print(result)``````

Here, I will convert the float value of string_with_decimal into an integer to remove the decimals. Then, convert that value into a string using the str() function.

``return str(int(float(string_with_decimal)))``

Output:

``3711``

Refer to the image below for the output of removing decimals from a string in Python using a custom function with string conversion.

## Conclusion

I hope the information in the above Python post helped you understand how to remove decimals from a string in Python.

Here, I have demonstrated several methods with some illustrative examples to remove decimals from a string in Python, such as the re.sub() method, split(), and join() methods, and math.floor() method, and string conversion.

You may also like to read the following articles related to Python: