06.0 Python‎ > ‎Python Notes‎ > ‎String Processing‎ > ‎

.split(), .splitlines(), .rsplit()

.split(), .splitlines(), .rsplit()

.split()
  • if no parameter is given, the string is split on each space.
  • if a parameter is given, for example ",", the string will be split in to a list on each comma

.splitlines()
  • will split a string into a list based on the EOL "\n" character

.rsplit()
  • right split -- 

.split(), .splitlines(), .rsplit() Syntax

list_variable = string_variable.split(sep)

sep is an optional parameter, if a character is given, the string_variable will be split on that character

.split(), .splitlines(), .rsplit() Examples

.split() Example:

data before :
This is a file with many words
and many lines

data = data.split()

data after 
['This', 'is', 'a', 'file', 'with', 'many', 'words', 'and', 'many', 'lines']
Notice: the data started as a string and was converted into a list


Comments