1 / 4

Strings and Lists – the split method

Strings and Lists – the split method. 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 ()

kalyca
Download Presentation

Strings and Lists – the split method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

More Related