When is str( ) useful?

Question

When is str( ) useful?

Answer

Often at times you’ll want to combine numbers with a string to display useful information on the screen. This is made possible by converting those numbers to strings so they can be printed together. If you wanted to get a user’s height in centimeters, for example, they’d enter a number as a string, that would need to be converted to a number to be used in a formula, and then converted back to a string using str( ) to be displayed in a sentence.

9 Likes

str() is very useful if you want to print numbers as strings. Say you wanted to combine some strings, and one of them was a number; you would have to use str() e.g print "Hello! I have " + str(6453) " views on my YouTube video!".

12 Likes

i know how useful str() is, but some people who are new to programming will say we can simple do the following:

print "Hello! I have 6453 views on my YouTube video!"

could you explain why this is not always possible? If not, let me know, i will add an explanation. Thank you :slight_smile:

9 Likes

Im a beginner and since there is no reply I would like to ask you that putting without str() is not possible sometimes because you would like something like a counter so you should put it like:

views = number_of_views (somehow that world like a view counter)
print “Hello! I have” + str(views) “views on my youtube video!”

if that is the case how you make a counter? Please message me back im avid to work in python.

2 Likes

yea, for example youtube has a counter of how many times a video is watched, which comes from a database. Then str() is possible to join integer and string, but there are also other possibilities

a database might be a bit complex, and to increase views every time the page is visit, but a simplified version could be achieved with a loop:

for views in range(10):
   print “Hello! I have” + str(views) + “views on my youtube video!”
10 Likes

Thanks so much now I understand it better!
:grin:

A little correction, don’t forget the second “+” operator in the example:

print “Hello! I have” + str(views) + “ views on my youtube video!”

thank you, a good reason why i don’t use + to concat string, but rather format. But then the example would no longer be valid for str()

Again you need to stress that var= always results in a string …some languages have looked at the characters and assumed integer fr instance…Python insists on you being specific – confused my little brain

can you give an example of python insist on being specific?

as may be the number varied with time or enterd by a user so must be equaled to variable .

what significance or slightest of difference does the code < print str(3.41).upper() > have from < print str(3.41) >

.upper() converts to uppercase:

print "abc".upper()

but numbers can’t be converted to uppercase, so using upper on a string containing numbers doesn’t do anyting

not even to the level of ASCII characters?

what do you mean?

ascii characters is everything, from letters to numbers to symbols. But the upper method only converts letters (a-z)

1 Like

How about :

a=6453
print(“Hello! I have”,a,“views on my YouTube video!”)

?

its not just about printing a string. String conversion can be useful at other times too.

i recently pulled an id (auto increment integer) from url and from database, these two id’s had to be compared, but the comparison was failing because of different data types (string and integer), so had to do a conversion. This is just one example, and i am sure there are many more

This lesson is about learning about data types and casting, sure, in this case you can get away with using , and let python handle the casting/conversion.

This will not work as intended. Try the following:

a=6453
print("Hello! I have %s views on my YouTube video!" % a)

Or alternatively, which was added in Python 3:

a=6453
print('Hello! I have {} views on my YouTube video!'.format(a))

Actually it prints just fine, but not as a single string. The values are not concatenated but comma separated in the print() argument.

Numbers cannot be concatenated with strings.

>>> a = 6453
>>> print ("Hello! I have " + a + " views on my YouTube video!")
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print ("Hello! I have " + a + " views on my YouTube video!")
TypeError: Can't convert 'int' object to str implicitly
>>> 

The str() constructor comes to the rescue in this instance…

>>> print ("Hello! I have " + str(a) + " views on my YouTube video!")
Hello! I have 6453 views on my YouTube video!
>>> 

There may be instances where such a statement is dynamic and string formatting is not convenient or workable for the progammer’s purposes. Having this function at our disposal addresses those situations.

As string formatting goes, Python coerces everything to string so we don’t have to re-cast values in the argument list. Witness the examples above.

I am very new to Python, and am struggling to clearly define the different data types, the very basics let’s say. :slight_smile:

What would be the difference between:
print “Hello! I have” + str(views) + “views on my youtube video!”
and:
print “Hello! I have” + int(views) + “views on my youtube video!”

Will the latter just not work as a string should be text rather than numbers?

2 Likes