Repository navigation
Expand file tree
/
Copy path032-String-to-Complex-Type-Casting.py
More file actions
78 lines (64 loc) · 3.39 KB
/
Copy path032-String-to-Complex-Type-Casting.py
File metadata and controls
78 lines (64 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
===============================================================================
String to Complex Type Casting
===============================================================================
Program Description:
--------------------
This program demonstrates how to convert a String data type into a
Complex data type using the complex() function.
The string must represent a valid integer, floating-point number,
or complex number.
Examples:
"100" ---> (100+0j)
"99.99" ---> (99.99+0j)
"5+3j" ---> (5+3j)
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 32-String-to-Complex-Type-Casting.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating a string variable.
# -----------------------------------------------------------------------------
string_value = "25+10j"
# string_value stores the string "25+10j".
# -----------------------------------------------------------------------------
# Displaying the original string value.
# -----------------------------------------------------------------------------
print("Original String Value :", string_value)
# Output: Original String Value : 25+10j
# -----------------------------------------------------------------------------
# Displaying the data type of the original variable.
# -----------------------------------------------------------------------------
print("Original Data Type :", type(string_value))
# Output: Original Data Type : <class 'str'>
# -----------------------------------------------------------------------------
# Converting the string into a complex number using complex().
# -----------------------------------------------------------------------------
complex_number = complex(string_value)
# complex_number stores the complex value (25+10j).
# -----------------------------------------------------------------------------
# Displaying the converted complex value.
# -----------------------------------------------------------------------------
print("Converted Complex Value :", complex_number)
# Output: Converted Complex Value : (25+10j)
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Converted Data Type :", type(complex_number))
# Output: Converted Data Type : <class 'complex'>
# -----------------------------------------------------------------------------
# Displaying the real part of the complex number.
# -----------------------------------------------------------------------------
print("Real Part :", complex_number.real)
# Output: Real Part : 25.0
# -----------------------------------------------------------------------------
# Displaying the imaginary part of the complex number.
# -----------------------------------------------------------------------------
print("Imaginary Part :", complex_number.imag)
# Output: Imaginary Part : 10.0
# -----------------------------------------------------------------------------
# Displaying an important note about String-to-Complex conversion.
# -----------------------------------------------------------------------------
print("Note: complex() converts a valid numeric string into a complex number.")
# Output: Note: complex() converts a valid numeric string into a complex number.