#πŸ”’ Confusion with printing variables

31 messages Β· Page 1 of 1 (latest)

thorny gale
#

Hello,

I recently started learning Python for Data Science purposes but I am already familiar with different coding concepts and the language Java.
Currently, I am trying to understand the data types in java. Weirdly, I cannot print them as expected:


    print("Unsorted list: " + scores.__str__())
    scores.sort()
    print("Sorted list: " + scores.__str__())

Why is it that the method (??) __str__ is necessary here? Is this even a method?
I have seen multiple occurrences of the double underscore but I haven't found any resources about what they mean.

I appreciate your help!

sharp zenithBOT
#

@thorny gale

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ripe pier
#

In short though, dunder methods are used to hook into internal stuff, like defining what an object's length is, and what subscripting means for that object.

thorny gale
thorny gale
ripe pier
#

You need str with your current code because +, for strings, requires that both operands be strings (Python is strongly typed).

thorny gale
#

That is what confused me because in java the toString method is usually called automatically

#

TypeError: can only concatenate str (not "list") to str

#

Aaaah wait, the comma

ripe pier
#

You're still using +.

thorny gale
#

πŸ˜“ Sorry πŸ˜„

ripe pier
#

Print is a bit of a special case. It's var arg, and converts all arguments to strings, and then by default, joins the arguments on a space.

thorny gale
#

Yup, that worked πŸ‘

thorny gale
ripe pier
#

!e

print('a', 'b', sep='')
print('a', 'b', sep='+')
sharp zenithBOT
#

@ripe pier :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | ab
002 | a+b
ripe pier
#

If you want more control though, just use f-strings.

#

!f-string

sharp zenithBOT
#
Format-strings

Creating a Python string with your variables using the + operator can be difficult to write and read. F-strings (format-strings) make it easy to insert values into a string. If you put an f in front of the first quote, you can then put Python expressions between curly braces in the string.

>>> snake = "pythons"
>>> number = 21
>>> f"There are {number * 2} {snake} on the plane."
"There are 42 pythons on the plane."

Note that even when you include an expression that isn't a string, like number * 2, Python will convert it to a string for you.

ripe pier
#

Yes, print is capable of a ton. It can even be used to write to files using its file parameter.

thorny gale
ripe pier
#

But yes, it's capable because it's made for convenience.

arctic helm
#

also 72% less typing :p

#

though we used to write "sout" and then eclipse was completing it or something

sharp zenithBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.