Download presentation
Presentation is loading. Please wait.
1
Strings and Lists – the split method
2
The split method for strings
This method is very powerful and is unique to Python It only applies to strings – remember this! The syntax is source.split(delimiter) or source.split() What does it do? it breaks the string into smaller pieces based on the delimiter string (if given) or using any whitespace character (if no delimiter given) It returns a list of strings
3
With no delimiters Suppose that str is “ abc \t def\t xyz “
str has lots of whitespace in it If you give the expression str.split() you get the result [“abc”, “def”, “xyz”] Note that the elements of the list do NOT have any whitespace in them at all Note that something like “ \t “ is used as one delimiter, not used individually (which would give you lots of empty strings in the list)
4
With delimiters Suppose str contained “abbabbadefbb” and you wrote the expression str.split(“bb”) You get the result [“a”,”a”,”adef”,””] Note that there is an empty string at the end of the list because the delimiter was found at the very end of the string (same thing happens if the delimiter is at the start of the string also) You are only allowed one string as a delimiter, it can be as long as you wish
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.