Welcome to Admin Junkies, Guest — join our community!

Register or log in to explore all our content and services for free on Admin Junkies.

Python How to do Typecasting and Spacing

Jason

Royal member
Bronze Member
Joined
Nov 3, 2022
Messages
1,446
Website
drummerlesson.com
Credits
31
When you are writing statements, you cannot display int, float, and bool stuff in a print statement. In that case, you need to use typecasting. Typecasting is converting one datatype to something else.

Code:
his_age = 30

print("His age is" + " " + str(his_age))

# The stuff above won't display without the typecasting because his_age is an int.

Let's look at examples with other datatypes.

Code:
he_can_play_guitar_or_not = True

# The above variable is a bool and can only only be true or false.

print ("It is" + " " + str(he_can_play_guitar_or_not) + " " + "that he can play guitar.")

Notice the + signs. Those make spaces in Python. In that case, the sentences won't run together.

Now, let's look at the float datatype. As noted before somewhere, it stores decimal versions of numbers.

Code:
the_gpa = 4.0

# ha ha, yeah right

print("The kid's gpa is" + " " +  str(the_gpa) + " " + "in elementary school.")

As we have seen, this typecasting is making things come thru with no errors. It is essential for Python. However, though, making little sentences isn't the only use for it. What else might it be used for? Any ideas?
 
Last edited:
# The stuff above won't display without the typecasting because his_age is an int.
Correct, you can't add a number to a string.

he_can_play_guitar_or_not = true # The above variable is a bool and can only only be true or false. print ("It is" + str(he_can_play_guitar_or_not) + "that he can play guitar.")
Won't run, because true is not a valid Python constant. But if you write it as True, however, it will run.

It will also print 'It isTruethat he can play guitar.' instead because + doesn't produce a space, it's a literal concatenate, not a clever one.

As we have seen, this typecasting is making things come thru with no errors. It is essential for Python. However, though, making little sentences isn't the only use for it. What else might it be used for? Any ideas?
Typecasting is very common, though making little sentences is probably *not* what you'd actually be doing with it (the typecasting merely enables it)

In practice you'd probably write it more like:
Code:
the_gpa = 4.0
print("The kid's gpa is {} in elementary school.".format(the_gpa))

Where the typecasting is sort of implicit and you don't get bitten by weird spacing by not having to remember to put the spaces in manually.
 
Correct, you can't add a number to a string.


Won't run, because true is not a valid Python constant. But if you write it as True, however, it will run.

It will also print 'It isTruethat he can play guitar.' instead because + doesn't produce a space, it's a literal concatenate, not a clever one.


Typecasting is very common, though making little sentences is probably *not* what you'd actually be doing with it (the typecasting merely enables it)

In practice you'd probably write it more like:
Code:
the_gpa = 4.0
print("The kid's gpa is {} in elementary school.".format(the_gpa))

Where the typecasting is sort of implicit and you don't get bitten by weird spacing by not having to remember to put the spaces in manually.
True stuff is fixed. How is it running now?
 
The + doesn't make a space, so it all runs together like I said...
 

Log in or register to unlock full forum benefits!

Log in or register to unlock full forum benefits!

Register

Register on Admin Junkies completely free.

Register now
Log in

If you have an account, please log in

Log in
Activity
So far there's no one here

Users who are viewing this thread

Would You Rather #9

  • Start a forum in a popular but highly competitive niche

    Votes: 5 21.7%
  • Initiate a forum within a limited-known niche with zero competition

    Votes: 18 78.3%
Win this space by entering the Website of The Month Contest

Theme editor

Theme customizations

Graphic Backgrounds

Granite Backgrounds