If you are unfamiliar with the operations that are in the question, my advice would be to try them out in a Python session. It will become immediately clear then.
I would further add, if you are having trouble understanding why you get the results you get after trying them out in a Python session, then I’d come back with the specific example, and I’m sure people would be happy to help clarify why the example returns the result in question.
thank you man but as a newbie can you fully explain to me with example?
Start interactive Python session (it is called REPL) and play with the expressions there:
$ python3 Python 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s = "testing string" >>> s+"" 'testing string'
When learning a new language, or even when learning programming in general, it is good to read about the language, but, personally, I’ve always found experimenting with the language to greatly enhance the learning experience.
@vbrozik above shows how easy it is to open up a Python session and then try out the various examples you are unfamiliar with. If you are on Windows, you may just need to type
python instead of
python3 (this is of course assuming you have Python already set up on your system). You will find that many of your questions will become quite obvious once you do this.
If you try them out and are still confused, feel free to ask questions about a specific example. I really do think that experimenting with code yourself is a valuable part of learning programming.
I would also add that it can be helpful to not just reproduce the example questions, but to then play around with other cases. Exploring helps make things clear. For instance, don’t just try
s + "", but maybe try what happens when you are adding two strings where one is not an empty string. What happens then?
➜ ~ python3 Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = "testing string" >>> s + "" 'testing string' >>> s + "another string" 'testing stringanother string'
Hopefully, the above makes it very clear how adding strings works without further explanation, but if you are still confused, feel free to ask specific related questions.
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s = File "<stdin>", line 1 s = IndentationError: unexpected indent >>> s = "hello world" >>> s+"" 'hello world' >>> ("a"+s)[1:] 'hello world' >>> s 'h' >>> s+s[1:] 'hello world' >>>
sure as I keep learning I will play with different cases
No problem, glad we could help .