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
 

猜你喜欢

转载自blog.csdn.net/u014222687/article/details/81159007