Get the App
SLTechnology News&Howtos  ›  Development  › 

How python splits words in a line

Shulou Source: shulou.com Published: 2022-06-01 05:25:25 09月25日 Update

This article mainly introduces how python splits words in one line, which is very detailed and has certain reference value. Friends who are interested must finish it!

Split words in a line

You don't need a special algorithm to split a word into a line. To do this, we can use the keyword split (). Here, I wrote two ways to segment words.

Method 1-use iteration

My_string = "This is a string in Python" start= 0 end= 0 my_list = [] for x in my_string: end=end+1 if (end'): my_list.append (my_ string [start: end+1]) start=end+1 my_list.append (my_ string [start: end+1]) print (my_list)

Output quantity

['This','is','a', 'string','in', 'Python']

Method II- uses the segmentation function

My_string = "This is a string in Python" my_list = my_string.split ('') print (my_list)

Output quantity

['This','is','a', 'string','in', 'Python'] these are all the contents of the article "how to split words in a line by python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Word line method content article output special for this purpose value key keyword interest function partner more knowledge algorithm industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Shulou Information Shulou Tech Info Redmi