Programs · Program 5-7
Q.Program 5-7: Example of type conversion between numbers and strings.
West Bengal WbchseTextbookSubjective· 2mImportance★★★★★est
29% · 14/49 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →str() converts a number into a string, and int() / float() convert a numeric string back into a number — as long as the string genuinely looks like that kind of number.
The idea (type conversion between numbers and strings). A string like "43" and the integer 43 are completely different values to Python — you cannot do arithmetic on the string or concatenation on the number. The bridge is explicit conversion: str() for number → string, and int() / float() for string → number. This matters constantly in real programs because input() always returns a string.
Program
# prog5-7.py
# Example of type conversion between numbers and strings
num = 27
numStr = str(num) # int -> str
print(numStr, type(numStr))
s1 = "43"
print(int(s1) + 7) # str -> int, then addition
s2 = "9.5"
print(float(s2) * 2) # str -> float, then multiplication
print(str(num) + s1) # both strings: concatenation
Expected output
27 <class 'str'>
50
19.0
2743
Key lines
str(27)gives the string"27"— it prints the same as the number, buttype()reveals it is<class 'str'>.int("43") + 7— the string is first converted to the integer 43, so the+is arithmetic: 50.float("9.5") * 2— string → float 9.5, then 9.5 × 2 = 19.0. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.