Python 之 str

import this  # Python之禪
'''
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''

import sys
print(sys.platform) # win32

from os import getcwd # current work directory
print(getcwd()) # C:\Users\zhouh\Desktop\PhythonTest

userInput = input("請輸入一個字符串,將除去重複字符:")
result = [] # 如果沒有這句話將報錯: name 'result' is not defined
# 請輸入一個字符串,將除去重複字符:iavadfbvdavbdahfvbhfdavbadhfvbahdfv
for ch in userInput:
    if ch not in result:
        result.append(ch)
print(result) # ['i', 'a', 'v', 'd', 'f', 'b', 'h']

'''
01:字符串的加法與乘法
02:去除字符串兩邊的空格(lstrip、rstrip、strip)
03:首字母大寫title()
04:全部取大寫或取小寫upper()、lower()
'''

# Happy 23 rd Birthdy
print("Happy " + str(23) + " rd Birthdy") #把整數轉化爲字符串

str0 = '  abc  '
print('H' + str0.rstrip() + 'H') # 去除右側的空格 # H  abcH
print('H' + str0.lstrip() + 'H') # Habc  H
print('H' + str0.strip() + 'H') # HabcH
print('H' + str0 + 'H') # H  abc  H


str1 = "hello world!"
print(str1*2) # 字符串的乘法 # hello world!hello world!

print(str1+str1.title()) # 字符串的加法  # hello world!Hello World!
str2 = str1.upper()
str3 = str1.lower()
print(str2+str3) # HELLO WORLD!hello world!

str4 = (str2+str3).title()
print(str4) # Hello World!Hello World!


# 轉義字符
str1 = "\"a\"" 
print(str1) # "a"

str2 = '\'b\''
print(str2) # 'b'

str3 = "'a'+'b'"
print(str3) # 'a'+'b'

str4 = '"a"+"b"'
print(str4) # "a"+"b"

str5 = "\'a\'+\"b\"+c" 
print(str5) # 'a'+"b"+c
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章