#๐Ÿ”’ Hi, really simple question. (beginner)

34 messages ยท Page 1 of 1 (latest)

lofty roostBOT
#

@rigid creek

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.

woeful night
#

What are you expecting to happen here?

rigid creek
#

cat

#

to program to print cat

woeful night
#

for that, you need print()

#

!d print

lofty roostBOT
#

print(*objects, sep=' ', end='\n', file=None, flush=False)```
Print *objects* to the text stream *file*, separated by *sep* and followed by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like [`str()`](https://docs.python.org/3/library/stdtypes.html#str) does and written to the stream, separated by *sep* and followed by *end*. Both *sep* and *end* must be strings; they can also be `None`, which means to use the default values. If no *objects* are given, [`print()`](https://docs.python.org/3/library/functions.html#print) will just write *end*.

The *file* argument must be an object with a `write(string)` method; if it is not present or `None`, [`sys.stdout`](https://docs.python.org/3/library/sys.html#sys.stdout) will be used. Since printed arguments are converted to text strings, [`print()`](https://docs.python.org/3/library/functions.html#print) cannot be used with binary mode file objects. For these, use `file.write(...)` instead.
woeful night
#

Try this:

spam = ['cat', 'bat', 'rat', 'elephant']
print(spam[0])
honest drift
#

The book may assume you're in the REPL.

#

In which case 'cat' would be displayed.

#

Without the print.

rigid creek
#

probably

#

i was losing my mind

#

but what if

#
spam = ['cat', 'bat', 'rat', 'elephant']
print(spam[0:2])
#

it prints it with [ and '

#

what should i do to remove that

#

['cat']

honest drift
#

How would you like it displayed?

rigid creek
#

cat

honest drift
#

There are different things you might do depending on how many things you're working with.

#

You've given one example but asked a question about something else that doesn't match the example.

#

I would suggest you learn about f-strings and str.format.

#

I would suggest learning about variable unpacking.

rigid creek
#

okay

honest drift
#

Including starred unpacking.

#

I would also suggest learning about starring arguments in print.

#

When you subscript like obj[0], you're getting the object at that index position. When you subscript with a slice, obj[:2] / obj[0:2], you're not getting the element, but (depending on obj's type) a list.

rigid creek
#

oh my bad

#

i just figured that

#

thank you a lot

lofty roostBOT
#
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.

#

๐Ÿ”’ Hi, really simple question. (beginner)