Python - String Splicing and Substrings Tutorial with Examples

Veröffentlicht am: 31 Oktober 2021
auf dem Kanal: Appficial
698
7

An index is an integer referencing a specific position in a string’s character sequence.

You can read multiple consecutive characters, or a substring, using slice notation. Slice notation has the form my_str[start:end], which creates a new string whose value contains the characters of my_str from indices start to end - 1.

url = '   / appficial   = url[8:23] # Read ‘youtube.com' from urlprint(domain)
Remember that strings are immutable in Python. Slice notation does not change the string but returns a new string returning the substring character sequence.

You can also add a 3rd argument in your slice notation called the stride, which sets the increment value after reading each element. For example, if it is set to 2, it will read every other character.

numbers = '0123456789'
print(f'All numbers: {numbers[::]}')
print(f'Even numbers: {numbers[::2]}')
print(f'Every 3rd number between 1 and 8: {numbers[1:9:3]}')

Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!


Auf dieser Seite können Sie das Online-Video Python - String Splicing and Substrings Tutorial with Examples mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Appficial 31 Oktober 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 698 Mal angesehen und es wurde von 7 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!