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

时间:2019-03-25 21:07:04

标签: python

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

string.swapcase();  

我知道如何用

大写所选文本:
string.capitalize();  

但是我想得到 cycle 小写字母-大写字母-所选文本的大写字母。
python可能吗?

编辑

  • 大写:文字;
  • 大写:TEXT;
  • 小写:文字。

可能的解决方案

### 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)  

此解决方案的问题是文本不会保持选中状态。

1 个答案:

答案 0 :(得分:1)

我的第一个解决方案

解决了!

# Get the current selection.
sText=clipboard.get_selection()
lLength=len(sText)

# Analyse.
if sText.islower():
    sText=sText.capitalize()
elif sText.isupper():
    sText=sText.lower()
else:
    sText=sText.upper()

# Send the result.
keyboard.send_keys(sText)
keyboard.send_keys("<shift>+<left>"*lLength)  

这有效! 但是大写字母只是第一个单词。

另一个更好的解决方案(混合的情况是 all 个单词)

# Get the current selection.
sText=clipboard.get_selection()
lLength=len(sText)

try:
    if not store.has_key("textCycle"):
        store.set_value("state","title")

except:
    pass

# get saved value of textCycle
state = store.get_value("textCycle")


# modify text and set next modfication style
if state == "title":
    #sText=sText.capitalize()
    sText=sText.title()
    newstate = "lower"

elif state == "lower":
    sText=sText.lower()
    newstate = "upper"

elif state == "upper":
    sText=sText.upper()
    newstate = "title"

else:
    newstate = "lower"

# save for next run of script
store.set_value("textCycle",newstate)   

# Send the result.
keyboard.send_keys(sText)
keyboard.send_keys("<shift>+<left>"*lLength)
相关问题