python根据分隔符查找子字符串

编程入门 行业动态 更新时间:2024-10-27 04:25:27
本文介绍了python根据分隔符查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 Python 新手,所以我可能会遗漏一些简单的东西.

I am new to Python, so I might be missing something simple.

我举了一个例子:

string = "The , world , is , a , happy , place "

我必须创建由 , 分隔的子字符串,并分别打印它们和处理实例.这意味着在这个例子中我应该能够打印

I have to create substrings separated by , and print them and process instances separately. That means in this example I should be able to print

The world is a happy place

我可以采取什么方法?我试图使用字符串查找功能,但是

What approach can I take? I was trying to use the string find functionality, but

Str[0: Str.find(",") ]

无助于查找第二个、第三个实例.

does not help in finding 2nd, 3rd instances.

推荐答案

尝试使用 split 功能.

在您的示例中:

string = "The , world , is , a , happy , place " array = string.split(",") for word in array: print word

您的方法失败了,因为您对其进行了索引以产生从头到第一个,"的字符串.如果您然后将它从第一个,"索引到下一个,"并以这种方式遍历字符串,这可能会起作用.不过拆分会好很多.

Your approach failed because you indexed it to yield the string from beginning until the first ",". This could work if you then index it from that first "," to the next "," and iterate across the string that way. Split would work out much better though.

更多推荐

python根据分隔符查找子字符串

本文发布于:2023-10-26 22:08:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   分隔符   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!