Why doesn’t Python automatically convert the number to a string?


#1

Question

Why doesn’t Python automatically convert the number to a string?

Answer

The answer becomes more clear as you learn to debug larger projects, and was carefully thought out when the language was being designed. In Python, and many other languages, the belief is that explicit is better than implicit. This means you should write your code with explicit functionality because by doing so, errors produced will help you understand where you went wrong.
For example, if we don’t convert a number to a string before concatenating it with a string, it gives a specific error that we are accustomed to looking for. However, if Python did this conversion automatically and, somehow, there was a bug in our program, it wouldn’t throw an error message because it converted as the language intended behind the scenes, but produced some unexpected output.


FAQ: Learn Python – Strings & Console Output – Explicit String Conversion
#2

hi
is using quotations the same as using the str() , or are there any differences?


#3

Using quotes is how we define a string. The str() function is for casting a non-string to a string.

str(42)            # '42'

str(True)          # 'True'

str([1,2,3,4,5])   # '[1, 2, 3, 4, 5]'

str({'one': 1})    # "{'one': 1}"

Eg.

def digit_sum(n):
    total = 0
    for x in str(n)
        total += int(x)
    return total

When n is 12345, str(n) will be ‘12345’, an iterable.


#4

This doesn’t makes exact sense considering Python supports implicit Data type conversion!


#5

In the context of for Python cannot implicitly create an iterable from an integer. Integers cannot be split.


#6

Hi mtf,

My comment was for this particular answer:
“The answer becomes more clear as you learn to debug larger projects, and was carefully thought out when the language was being designed. In Python, and many other languages, the belief is that explicit is better than implicit. This means you should write your code with explicit functionality because by doing so, errors produced will help you understand where you went wrong.”


#7

If errors occur in an explicit, declarative environment, they occur for a specific reason. Loosely typed environments need tight reins, aka, nearly explicit. They must be able to safely morph one type to another in a meaningful way. We are still in charge of providing that meaning.

There can be no question as to the value of explicitness. What is, is. Implicitness is rife with gray area that needs to be thought over.

JavaScript arrow functions, and Ruby methods share one thing in common… Implicit return.

const f = x => x ** 2;
 >  const f = x => x ** 2
>- undefined
 > f(2)
<- 4
 > f(3)
<- 9
 > f(4)
<- 16

#8

I guess I understand what you are trying to say. However, your answer will make sense only to a seasoned programer who is trying to pick up Python as a new laguage; like me.

For a newbie, it doesnt makes much sense. Also, without going in with a bit detailed explanation it sounds wrong to say that Python expects the Programmer to do explicit data casting for functions & operators which is not really the case since you can add a float & integer to get a float in python unlike some other languages.


#9

What ever I say is refutable. That is stipulated. Discussion should emerge and then form a web. I’ve not got stuck to that yet, but hey, stuff happens.


#10

I like explicit having worked with implicit languages like 4GLs but it is still confusing to a Python newbie e.g. var = 3 then 3 is a string but string + 2 + string gives an error unless I put in str(2) …which implies that the interpreter assumed the 2 was an integer!


#11

if i do:

var = 3
print type(var)

we can clearly see it gives integer as response. If you do:

var = "3"
print type(var)

then you have a string