In the previous article, we have discussed Python String strip() Method Examples
split() Method in Python:
Splitting a string into a list is accomplished by the split() method.
The separator can be specified; the default separator is any whitespace.
Note: When maxsplit is specified, the list will have the number of elements specified plus one.
Syntax:
string.split(separator, maxsplit)
Parameters
separator: This is Optional. This parameter specifies the separator to be used when splitting the string. By default, any whitespace serves as a separator.
maxsplit: This is Optional. Specifies the number of splits to perform. The default value is -1, which represents “all occurrences.”
Examples:
Example1:
Input:
Given string = "welcome,to,python,programs" Given separator = "," Given maxsplit value = 1
Output:
The above given string is : welcome,to,python,programs The given string after applying split() function: ['welcome', 'to,python,programs']
Example2:
Input:
Given string = "good morning this is btechgeeks"
Output:
The above given string is : good morning this is btechgeeks The given string after applying split() function: ['good', 'morning', 'this', 'is', 'btechgeeks']
String split() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the separator as static input and store it in another variable.
- Give the maxsplit as static input and store it in another variable.
- Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
- Store it in another variable.
- Print the above-given string.
- Print the above-given string after applying the split() function.
- Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "welcome,to,python,programs" # Give the separator as static input and store it in another variable. gvn_separtr = "," # Give the maxsplit as static input and store it in another variable. maxsplt_valu = 1 # Apply split() method to the given string for the given separator and # maxsplit() values which split a string into a list. # Store it in another variable. rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying split() function. print("The given first string after applying split() function:") print(rslt_lst1) # Similarly do the same for other string without giving the separator and # maxsplit values and print the result list obtained. gvn_scndstr = "good morning this is btechgeeks" rslt_lst2 = gvn_scndstr.split() print("The above given second string is :", gvn_scndstr) print("The given second string after applying split() function:") print(rslt_lst2)
Output:
The above given first string is : welcome,to,python,programs The given first string after applying split() function: ['welcome', 'to,python,programs'] The above given second string is : good morning this is btechgeeks The given second string after applying split() function: ['good', 'morning', 'this', 'is', 'btechgeeks']
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the separator as user input using the input() function and store it in another variable.
- Give the maxsplit as user input using the int(input()) function and store it in another variable.
- Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
- Store it in another variable.
- Print the above-given string.
- Print the above-given string after applying split() function.
- Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some random string = ") # Give the separator as user input using the input() function and store it in another variable. gvn_separtr = input("Enter some random separator = ") # Give the maxsplit as user input using the int(input()) function and store it in another variable. maxsplt_valu = int(input("Enter some random number = ")) # Apply split() method to the given string for the given separator and # maxsplit() values which split a string into a list. # Store it in another variable. rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying split() function. print("The given first string after applying split() function:") print(rslt_lst1) # Similarly do the same for other string without giving the separator and # maxsplit values and print the result list obtained. gvn_scndstr = input("Enter some random string = ") rslt_lst2 = gvn_scndstr.split() print("The above given second string is :", gvn_scndstr) print("The given second string after applying split() function:") print(rslt_lst2)
Output:
Enter some random string = hello#this is btechgeeks Enter some random separator = # Enter some random number = 1 The above given first string is : hello#this is btechgeeks The given first string after applying split() function: ['hello', 'this is btechgeeks'] Enter some random string = hello all good morning The above given second string is : hello all good morning The given second string after applying split() function: ['hello', 'all', 'good', 'morning']
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.