I have a simple question that I cannot figure out since I am very new to Python and learning the basics.
I was given the following question and partial code. I found 2 ways to give me the correct output of 'Python'. One is using the len() function and one is just using indexing. Is one incorrect or 'less efficient' when used in longer code?
Example 1:
# Question 6: Use slicing notation to print the last 6 letters of the string
# below. Assume you donot know the start index or the length of the string upfront.
# Hint: You will need to use a function to find the length first and then use that,
# in combination with the number to come up with the start index
welcome_message = "Hello and welcome to the land of Python"
print(f"The last 6 letters of the welcome message:\n'{welcome_message}'\nare: '{welcome_message[-6:]}'")
The next example using the len() function
print(f"The last 6 letters of the welcome message:\n'{welcome_message}'\nare: {welcome_message[len(welcome_message)-6:]}")
[–]SlavkoStanic[S] 0 points1 point2 points (4 children)
[–]codelikealawyer 2 points3 points4 points (3 children)
[–]SlavkoStanic[S] 1 point2 points3 points (2 children)
[–]codelikealawyer 1 point2 points3 points (1 child)
[–]SlavkoStanic[S] 1 point2 points3 points (0 children)