Skip to main content

Posts

Showing posts from December, 2022

Split & Join Strings in Python

Split & Join Strings in Python In Python, strings are sequences of characters. Various built-in functions are available to work with strings.  We often come across the need to split the string into a list of substrings (with use of a delimiter character to split the string) and to join a list of strings into a single string. Splitting a String We can use the string method 'split()' to split a string into a list of substrings. This method by default split the string into a list of substrings which are separated by  blank space.  In the below example, we are calling split() method without passing any argument. 1 2 3 4 sample_string = "This is a  string" sub_strings = sample_string.split() print (sub_strings)   Below is the result.  ['This', 'is', 'a', 'string'] If we notice the string, there are two blank spaces between "a" and "string" and same is not present in the substring list. We can also pass the specific de...