字符串循环大小写(小写 - 大写 - 大写)

我知道如何在 python 中将选定的文本从小写转换为大写,反之亦然:使用


string.swapcase();  

而且我知道如何将所选文本大写:与


string.capitalize();  

但我想得到一个循环小写 -大写- 所选文本的大写。

可以用python吗?


编辑

  • 大写:文本;

  • 大写:文本;

  • 小写:文本。

可能的解决方案

### Get the current selection.  

sText=clipboard.get_selection()  


### Analyse.  

if sText.islower():  

    sText=sText.capitalize()  

elif sText.isupper():  

    sText=sText.lower()  

else:  

    sText=sText.upper()  


### Send the result.  

keyboard.send_keys(sText)  

这个解决方案的问题是文本没有保持选中状态。


犯罪嫌疑人X
浏览 164回答 1
1回答

白板的微信

我的第一个解决方案解决了!# Get the current selection.sText=clipboard.get_selection()lLength=len(sText)# Analyse.if sText.islower():&nbsp; &nbsp; sText=sText.capitalize()elif sText.isupper():&nbsp; &nbsp; sText=sText.lower()else:&nbsp; &nbsp; sText=sText.upper()# Send the result.keyboard.send_keys(sText)keyboard.send_keys("<shift>+<left>"*lLength)&nbsp;&nbsp;这有效! 但是大写的文本只是第一个单词。另一个更好的解决方案(混合大小写是所有单词)# Get the current selection.sText=clipboard.get_selection()lLength=len(sText)try:&nbsp; &nbsp; if not store.has_key("textCycle"):&nbsp; &nbsp; &nbsp; &nbsp; store.set_value("state","title")except:&nbsp; &nbsp; pass# get saved value of textCyclestate = store.get_value("textCycle")# modify text and set next modfication styleif state == "title":&nbsp; &nbsp; #sText=sText.capitalize()&nbsp; &nbsp; sText=sText.title()&nbsp; &nbsp; newstate = "lower"elif state == "lower":&nbsp; &nbsp; sText=sText.lower()&nbsp; &nbsp; newstate = "upper"elif state == "upper":&nbsp; &nbsp; sText=sText.upper()&nbsp; &nbsp; newstate = "title"else:&nbsp; &nbsp; newstate = "lower"# save for next run of scriptstore.set_value("textCycle",newstate)&nbsp; &nbsp;# Send the result.keyboard.send_keys(sText)keyboard.send_keys("<shift>+<left>"*lLength)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python