Monday, March 7, 2022

Split() method in Python

Split() method:

  • Splits the string into a list of words. 
  • You can specify a separator (e.g ' '' !)
  • The default separator is whitespace.
  • We can take string input from the user. Use the split method and add the input to dictionary key-value pairs.

For example:

for i in range(n):
name=input()
phoneNum=name.split()
PhoneBook[phoneNum[0]]= phoneNum[1]

Another example:

string="Hello click this link"
list=string.split()
print(list)
OUTPUT:
['Hello', 'click', 'this', 'link']

Process finished with exit code 0

No comments:

Post a Comment