顯示廣告
隱藏 ✕
※ 本文為 terievv 轉寄自 ptt.cc 更新時間: 2017-08-29 20:56:58
看板 Gossiping
作者 ycl94 (Orz)
標題 Re: [問卦] C程式大神們請進
時間 Tue Aug 29 04:43:07 2017


※ 引述《Roger0123 (Roger)》之銘言:
: ※ 引述《ptt0720 (濕濕)》之銘言:
: : ##
: : string = input()
: : ansStr =''
: : for i in string:
: :         ansStr += i.upper() if 'a'<=i<='z' else i.lower()
: : print(ansStr)
: : ##
: Python 的話
: 用 list comprehension 可以一行解決
: print(''.join(c.upper() if c.islower() else c.lower() for c in input()))
: 感謝鄉民提醒,因為中文字不到30,所以補個測試結果吧:
: Test因為中文字不夠30只好補個測試結果
: tEST因為中文字不夠30只好補個測試結果
如果Python可以參戰,那用string函數: swapcase()即可.

ycl94 @ MBA [:][~] python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> raw_input().swapcase()
pTt teStIng
'PtT TEsTiNG'

為了滿足30個字的需求, 耍廢說明一下 swapcase()就如同這函數名稱,
就是將大小寫交換(swap), 所以小寫就變大寫, 反之亦然 :P

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.11.51
※ 文章代碼(AID): #1Pf81T8v (Gossiping)
※ 文章網址: https://www.ptt.cc/bbs/Gossiping/M.1503952989.A.239.html
※ 編輯: ycl94 (111.249.11.51), 08/29/2017 04:43:46
formatted: 我最喜歡python惹1F 08/29 04:43
wasijohn: :p2F 08/29 04:44
※ 編輯: ycl94 (111.249.11.51), 08/29/2017 04:49:13
HowLeeHi: 從C到C++、java、python..等一下Obj-C,C#要出來了嗎3F 08/29 04:57
HowLeeHi: julia,golang,rust,swift,perl,php,ruby,js,kotlin,asm
lauly: PowerShell 也來參一腳5F 08/29 05:06
tinlans: 無聊死了  一點八卦都沒有  來睡6F 08/29 05:06
johnhmj: 標題可改為「我與大蟒蛇的情愛初體驗」7F 08/29 05:26
freeunixer: 全八卦板上沒女友共度情人節的肥宅全部蛇出來了 (~誤8F 08/29 05:30
kyle5241: 原本想說用字典的9F 08/29 06:23
AirLee: 好慘 這種日子只能回這種文10F 08/29 07:36
jason710068: Python棒棒11F 08/29 08:18
alan23273850: 這個不錯12F 08/29 08:56

--
※ 看板: terievv 文章推薦值: 0 目前人氣: 0 累積人氣: 152 
作者 ycl94 的最新發文:
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇