我想將"Onehundredthousand"
拆分爲"one"
"hundred"
"thousand"
使用python。 我該怎麼做?將字符串拆分爲python中的單獨字符串
2
A
回答
5
>>> s = "Onehundredthousand"
>>> s.replace('hundred', '_hundred_').split('_')
['One', 'hundred', 'thousand']
這隻對給定的字符串有效。
4
使用正則表達式re.split
。如果您使用捕獲組作爲分隔符,它也將被包括在結果列表:
>>> import re
>>> re.split('(hundred)', 'Onehundredthousand')
['One', 'hundred', 'thousand']
+0
感謝您的支持 –
6
您可以使用一個字符串的partition
方法將其分爲3個部分(左部分,分離器,右邊部分):
"onehundredthousand".partition("hundred")
# output: ('one', 'hundred', 'thousand')
相關問題
- 1. 將字符串拆分爲兩個單獨的字符串
- 2. 將字符串拆分爲字符串
- 3. 將字符串拆分爲字符串
- 4. C#拆分字符串 - 將字符串拆分爲數組
- 5. Java:將字符串拆分爲單獨的字符串和整數變量
- 6. 將字符串拆分爲「|」
- 7. 拆分字符數組爲單獨的字符串
- 8. 將lua字符串拆分爲字符
- 9. 將表空間中的字符串拆分爲單獨的列
- 10. 如何將json字符串拆分爲mysql中的單獨行
- 11. Python的字符串拆分
- 12. 如何將字符串拆分爲java中的子字符串
- 13. 如何將字符串拆分爲iOS中的子字符串?
- 14. 將CSV字符串拆分爲PHP中的多個字符串
- 15. 如何在python中將字符串拆分爲字符?
- 16. 將字符串生成器拆分爲字符串字符串特定字符
- 17. Python字符串拆分
- 18. Python字符串拆分
- 19. 拆分字符串在python
- 20. Python字符串拆分
- 21. Python拆分字符串
- 22. 如何將字符串拆分爲字母字符串和數字字符串?
- 23. 將字符串拆分爲兩個單獨的屬性ruby
- 24. Elisp拆分字符串函數來拆分字符串。字符
- 25. 在iOS中將字符串拆分爲子字符串
- 26. 將字符串拆分爲字典
- 27. 如何將分號分隔的字符串拆分爲字符串中的單獨項目?
- 28. 如何將字符串拆分爲等長的子字符串?
- 29. 如何將字符串拆分爲重複的子字符串
- 30. 將字符串拆分爲多個較小的字符串
post ur attempts .. –
只是對於這個特定的字符串,可以有n種不同的解決方案。但是如果你想要一個通用的解決方案,你需要有一些分隔符。 –