![](/img/trans.png)
[英]How to convert characters of a string from lowercase to upper case and vice versa without using string functions in python?
[英]How to convert specific character sequences in a string to upper case using Python?
我希望完成以下內容,並且想知道是否有人建議如何最好地去做。
我有一個字符串,說'this-is,-toronto.-and-this-is,-boston',我想將所有出現的', - [az]'轉換為', - [AZ]'。 在這種情況下,轉換的結果將是'this-is,-Toronto.-and-this-is,-Boston'。
我一直在嘗試使用re.sub(),但尚未弄清楚如何使用
testString = 'this-is,-toronto.-and-this-is,-boston'
re.sub(r',_([a-z])', r',_??', testString)
謝謝!
re.sub可以使用一個返回替換字符串的函數:
import re
s = 'this-is,-toronto.-and-this-is,-boston'
t = re.sub(',-[a-z]', lambda x: x.group(0).upper(), s)
print t
版畫
this-is,-Toronto.-and-this-is,-Boston
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.