Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python splits words in a line

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report