Repository navigation
Expand file tree
/
Copy path033-String-Integer-to-All-Basic-Data-Types.py
More file actions
132 lines (108 loc) · 5.19 KB
/
Copy path033-String-Integer-to-All-Basic-Data-Types.py
File metadata and controls
132 lines (108 loc) · 5.19 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
===============================================================================
String Integer to All Basic Data Types
===============================================================================
Program Description:
--------------------
This program demonstrates how to convert a string containing an integer
value into all basic Python data types.
Conversions demonstrated:
1. String to Integer
2. String to Float
3. String to Boolean
4. String to String
5. String to Complex
Example:
"100" → 100
"100" → 100.0
"100" → True
"100" → "100"
"100" → (100+0j)
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 33-String-Integer-to-All-Basic-Data-Types.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating a string variable containing an integer value.
# -----------------------------------------------------------------------------
string_value = "100"
# string_value stores the string "100".
# -----------------------------------------------------------------------------
# Displaying the original string value.
# -----------------------------------------------------------------------------
print("Original String Value :", string_value)
# Output: Original String Value : 100
# -----------------------------------------------------------------------------
# Displaying the original data type.
# -----------------------------------------------------------------------------
print("Original Data Type :", type(string_value))
# Output: Original Data Type : <class 'str'>
# -----------------------------------------------------------------------------
# Converting the string into an integer.
# -----------------------------------------------------------------------------
integer_value = int(string_value)
# -----------------------------------------------------------------------------
# Displaying the integer value and its data type.
# -----------------------------------------------------------------------------
print("\nString to Integer")
print("Converted Value :", integer_value)
# Output: Converted Value : 100
print("Data Type :", type(integer_value))
# Output: Data Type : <class 'int'>
# -----------------------------------------------------------------------------
# Converting the string into a float.
# -----------------------------------------------------------------------------
float_value = float(string_value)
# -----------------------------------------------------------------------------
# Displaying the float value and its data type.
# -----------------------------------------------------------------------------
print("\nString to Float")
print("Converted Value :", float_value)
# Output: Converted Value : 100.0
print("Data Type :", type(float_value))
# Output: Data Type : <class 'float'>
# -----------------------------------------------------------------------------
# Converting the string into a Boolean.
# -----------------------------------------------------------------------------
boolean_value = bool(string_value)
# -----------------------------------------------------------------------------
# Displaying the Boolean value and its data type.
# -----------------------------------------------------------------------------
print("\nString to Boolean")
print("Converted Value :", boolean_value)
# Output: Converted Value : True
print("Data Type :", type(boolean_value))
# Output: Data Type : <class 'bool'>
# -----------------------------------------------------------------------------
# Converting the string into another string.
# -----------------------------------------------------------------------------
new_string_value = str(string_value)
# -----------------------------------------------------------------------------
# Displaying the string value and its data type.
# -----------------------------------------------------------------------------
print("\nString to String")
print("Converted Value :", new_string_value)
# Output: Converted Value : 100
print("Data Type :", type(new_string_value))
# Output: Data Type : <class 'str'>
# -----------------------------------------------------------------------------
# Converting the string into a complex number.
# -----------------------------------------------------------------------------
complex_value = complex(string_value)
# -----------------------------------------------------------------------------
# Displaying the complex value and its data type.
# -----------------------------------------------------------------------------
print("\nString to Complex")
print("Converted Value :", complex_value)
# Output: Converted Value : (100+0j)
print("Data Type :", type(complex_value))
# Output: Data Type : <class 'complex'>
# -----------------------------------------------------------------------------
# Displaying an important note.
# -----------------------------------------------------------------------------
print("\nNote: A string containing a valid integer value can be converted")
print("to all basic Python data types using their respective functions.")
# Output:
# Note: A string containing a valid integer value can be converted
# to all basic Python data types using their respective functions.