#๐ Hi, really simple question. (beginner)
34 messages ยท Page 1 of 1 (latest)
@rigid creek
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.
What are you expecting to happen here?
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.
Try this:
spam = ['cat', 'bat', 'rat', 'elephant']
print(spam[0])
The book may assume you're in the REPL.
In which case 'cat' would be displayed.
Without the print.
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']
How would you like it displayed?
cat
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.
okay
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.
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)