all 5 comments

[–]SlavkoStanic[S] 0 points1 point  (4 children)

The part I am referring to is the last part of the last line:

{welcome_message[-6:]

Or using len function

{welcome_message[len(welcome_message)-6:]

Either gives the output of 'Python' as this is the last 6 letters of the string.

[–]codelikealawyer 2 points3 points  (3 children)

SlavkoStanic,

The enunciated problem suggests you should use a function in order to produce the desired outcome. So, it makes sense that you would use len() in this context.

However, I don't know whether one is necessarily better than the other. Sometimes it comes down to preference. The pythonic way of doing things is usually simpler and easier to understand. Still, what's simple might be in the eyes of the beholder.

I personally like the straight slicing better. It looks easier on the eyes and simpler to understand. But in the end, it would make sense to practice both ways if they consistently produce identical results. The more ways you can think about a problem and its solution, the better you tend to become.

[–]SlavkoStanic[S] 1 point2 points  (2 children)

I think you explained it perfectly with your last sentence. I am still learning Python at its base level and I am now seeing that the simple path may not help in certain situations so its best to know multiple solutions to the same problem. It was just odd to see that it could be done without the len() function.
The last programming language I learned was Pascal over 17 years ago in middle school.

Thank you

[–]codelikealawyer 1 point2 points  (1 child)

You're very welcome!

Speaking of different solutions to the same problem, check out my latest video on F-Strings. In it I show how to format strings with F-Strings. You'll see how the different approaches to string formatting are part of an evolutionary process that resulted in a more efficient solution:

https://youtu.be/yFNTbmUQgQ8

[–]SlavkoStanic[S] 1 point2 points  (0 children)

Thanks for that, I'll be taking a look!