Open In App

Convert Complex Number to String in Python

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given complex numbers and we have to convert these complex numbers into strings and return the result in string form. In this article, we will see how we can convert complex numbers to strings in Python.

Examples:

Input : 3+4j 
<complex>
Output : 3+4j
<string>
Explanation: We can see that we converted 3+4j complex to string 3+4j

Complex Number to String in Python

Below, are the methods of converting Python Complex to String in Python:

  • Using str() Function
  • Using format() Method
  • Using f-strings
  • Using repr() Function

Python Complex to String Using str() Function

In this example, the below code creates a complex number (3 + 4j) and converts it to a string using the `str()` function. The type of the resulting string representation is then printed, demonstrating that it is a string.

Python3




# Example
complex_number = 3 + 4j
string_representation = str(complex_number)
 
print(type(complex_number))
print("String representation using str():", string_representation)
print(type(string_representation))


Output

<class 'complex'>
String representation using str(): (3+4j)
<class 'str'>

Python Complex to String Using format() Method

In this example, below code creates a complex number (3 + 4j) and formats it into a string with two decimal places for real and imaginary parts using the `format()` method. The formatted string is then printed, and its type (string) is also displayed.

Python3




# Example
complex_number = 3 + 4j
formatted_string = "Real: {:.2f}, Imaginary: {:.2f}".format(complex_number.real, complex_number.imag)
 
print(type(complex_number))
print("Formatted string using format():", formatted_string)
print(type(formatted_string))


Output

<class 'complex'>
Formatted string using format(): Real: 3.00, Imaginary: 4.00
<class 'str'>

Python Complex to String Using f-strings

In this example, below code creates a complex number (3 + 4j) and uses an f-string to format it into a string. The formatted string is then printed, and its type (string) is also displayed.

Python3




# Example
complex_number = 3 + 4j
formatted_string = f"Complex number: {complex_number}"
 
print(type(complex_number))
print("Formatted string using f-strings:", formatted_string)
print(type(formatted_string))


Output

<class 'complex'>
Formatted string using f-strings: Complex number: (3+4j)
<class 'str'>

Python Complex to String Using repr() Function

In this example, below code creates a complex number (3 + 4j) and obtains its string representation using the `repr()` function. The resulting string is then printed, and its type (string) is displayed.

Python3




# Example
complex_number = 3 + 4j
repr_string = repr(complex_number)
 
print(type(complex_number))
print("String representation using repr():", repr_string)
print(type(repr_string))


Output

<class 'complex'>
String representation using repr(): (3+4j)
<class 'str'>

Conclusion

Converting complex numbers to strings in Python is a common task, and there are several easy and effective methods to achieve this. Whether you prefer simplicity, customization, or detailed representation, the methods discussed in this article provide flexibility to suit various needs. Understanding these techniques will empower you to handle complex numbers in Python with ease.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads