Python Forum
Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy)
#1
The purpose of the exercise is to take a string which says: "happy_birthday" and then use slicing to have the interpreter print just ‘happy’.

Take a look at these Python shell outputs:

>>> string = "happy_birthday" 
>>> string[:]
'happy_birthday'
>>> string
'happy_birthday'
>>> string[:'birthday']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method
>>> string[ :string.index("_") ]
'happy'
>>> string[0 : string.index("birthday") ]
'happy_'
Line 6 is invalid because it’s not possible for slicing arguments to include strings. But line 10 takes a string as an argument to slice with and it is valid. Why are strings invalid (like at line 6) while other times parses as valid (like at line 10)?

My feeble answer to my own question is that programming in general involves symbol attribution like in algebra where words or symbols invoke an operation that substitute one symbol with another. So in my case the syntax of string.index(“_”) calls the index method on the string variable which refers to the placement of the underscore which in this case turns out to be position 5 of the given string "happy_birthday". So to complete the slice, "happy_birthday" is read as just "happy” because the slice starts at 0 and ends at the position 5.

Is this mostly correct? Or am I way off? How might one of you correct or add to my explanation here?

For my future reference, this Udemy instructor Ziyad Yehia refers to this code in "Quiz 3: Slices Quiz". This exercise is in between module #27 and 28 under Section #5. The course is titled "The Python Bible: Everything You Need to Program in Python". This is a course which is not for credit. It's just for fun.
Reply


Messages In This Thread
Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) - by Drone4four - Aug-23-2019, 08:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 613 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,572 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Passing string functions as arguments Clunk_Head 3 1,266 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Splitting strings in list of strings jesse68 3 1,773 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 8,057 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  TypeError: not enough arguments for format string MaartenRo 6 2,944 Jan-09-2022, 06:46 PM
Last Post: ibreeden
  String slicing and loop iteration divyansh 9 4,750 Jun-07-2020, 10:29 PM
Last Post: divyansh
  String slicing divyansh 6 3,374 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty
  question: finding multiple strings within string djf123 4 2,994 May-16-2020, 01:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020