python - 在一些字符后替换字符串

标签 python string

如何替换字符串中的子字符串?例如我有字符串:

string1/aaa
this is string2/bbb
string 3/ccc
this is some string/ddd

我想读取“/”之后的子字符串。我需要这个输出:

aaa
bbb
ccc
ddd

谢谢。

最佳答案

可以拆分字符串获取数据

my_string.split("/")[1]

例如,

data = ["string1/aaa", "this is string2/bbb", "string 3/ccc",
        "this is some string/ddd"]    
print [item.split("/")[1] for item in data]

输出

['aaa', 'bbb', 'ccc', 'ddd']

关于python - 在一些字符后替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934013/

相关文章:

python - 正则表达式子函数不适用于 unicode 字符串

python - 两个 sqlalchemy 查询返回相同的结果,即使它们具有不同的过滤器

c++ - 关于字符串和函数的问题。

python - 从领先的某些实例中清除列表

python - 空记录数组的单元测试相等性

python - 仅使用Python 2.7内置模块计算子网中的所有IP

Java 1.4 String.replaceAll 单引号问题

javascript - 带分割的字符串格式占位符用于分隔字符串

c++ - 编译时字符串数组错误

r - 匹配 switch 语句中的字符串