+ 3
Guys I have question
What is casting type? And what is the purpose of casting type ?
8 Answers
+ 7
casting is the process of converting a 'data type' from one form to another. Such as this converting strings (text of numbers surrounded by quotes) into numbers or integers (whole numbers)/ floats (decimal numbers). For example, the string "543123" can be cast to the integer 543123 or the float 543123.0
The opposite is to convert or cast a float (decimal number) to an integer (whole number) casting to a string of text
You cannot directly use a string of number "543123" and multiple or divide it without casting it to an integer or float or a double
By understanding and effectively using casting, you can write more flexible and powerful programs that can handle diverse data types and perform a wide range of operations.
+ 5
sometimes we get data in a type that's not ideal to perform operations in. For example, user inputs usually return string data type. If we want to perform math operations on it, we need to cast it to a numeric type. Or if a math operation results in a float or double but we need int. Or if we need to display or store numeric data in a file but the file format or display media only accepts strings.
+ 5
Karima Khadri say you have a data type that is a string but part of the string is a numerical value and we need to separate the string into usable data types from a string not several inputs but one string.
Here is an example a walk through example.
x = "Taylor has an account balance of 3500 dollars in the bank"
words = x.split()
for i, word in enumerate(words):
# this is where we found 3500
if word.isdigit():
# we casted the word from a string and made it an int (a number)
new_balance = int(word) + 1500
# we are now casting the new_balance as a string to go back into our original full string at a specific index [i]
words[i] = str(new_balance)
break
# we are now restructuring the full sentence to include the new balance at the original index.
new_string = " ".join(words)
print(new_string)
# Output: Taylor has an account balance of 5000 dollars in the bank
+ 3
Check out this link. It will give you detailed info along with examples and faq's related to type casting.
https://www.geeksforgeeks.org/type-casting-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
+ 3
Aljimar Amman kindly don't spam in other's questions. If you have any issues regarding your code please create a new question with detailed info and attach the code with relevant tags.
+ 1
I understad you , but why canâT just write int or float directly if we goal this instead of writing , for example string â1008 â and cast it to int
0
I need help guys I'm a beginner because my coding is error
0
I never know how can I learn