#!/usr/localbin/python3
# -*- coding: utf-8 -*-
# https://www.cnblogs.com/wupeiqi/articles/5444685.html
# 結(jié)果是值的分成下面兩類
# 算數(shù)運算符 賦值運算符
# ========================
# 結(jié)果是布爾值的分成下面三類
# 比較運算符 邏輯運算符 成員運算符
# 基本數(shù)據(jù)類型
## 數(shù)字
## 字符串
## 布爾值
# python3 只有int 整形之說
# int str list tuple dict bool
# int --->? 數(shù)字 所有的功能,都放在int里
"""
字符串轉(zhuǎn)換為數(shù)字類型
進(jìn)制轉(zhuǎn)換
num ="0011"
v = int(num, base=16)
print(v)
age = 5
r = age.bit_length()
print(r)
bit_length 當(dāng)前數(shù)字的二進(jìn)制至少用幾位來表示
所有變小寫,只是casefold更牛逼,他有很多的未知對應(yīng)關(guān)系
v = test.center(20)
print(v)
表示有20個位置并將內(nèi)容居中,后面沒有就是默認(rèn)是空白
v = test.center(20,"*") 后面有* 就是使用*填充
endswith startswith 開頭和結(jié)束
encode decode? 編碼和解碼
find 從前往后找
format
test = 'i am {name}'
print(test)
v = test.format(name='root')
print(v)
index 索引的位置
isalnum 判斷是不是只包含字母和數(shù)字 ,都是數(shù)字也可以,都是字母也可以
isupper
"""
# capitalize 是將首字母變大
n = 'root'
print(n)
v = n.capitalize()
print(v)
print('===========capitalize==========')
# center:內(nèi)容居中,width:總長度, fillchar:空白處需要填充的內(nèi)容,不填默認(rèn)是空白
#? def center(self, width, fillchar=None):
n = 'cntf'
print(n)
v = n.center(10)
v = n.center(30,"-")
print(v)
print('==========center===========')
# count 子序列個數(shù),判斷有多少個字符
# 例如:str.count(sub, start=0,end=len(string))
# 函數(shù):def count(self, sub, start=None, end=None):
# sub -- 搜索的子字符串
# start -- 字符串開始搜索的位置,默認(rèn)為第一個字符串,第一個字符索引值為0
# end -- 字符串中結(jié)束搜索的位置,
n = 'this is string test cntf ... hehe'
print(n)
v = n.count('he', 0, 50)
print(v)
print('==========count===========')
# endswith 是否以....結(jié)束....
# def endswith(self, suffix, start=None, end=None):
n = 'this is cntf test python3'
print(n)
# v = n.endswith('on3')
v = n.endswith('tf', 0, 12)
print(v)
print('==========endswith===========')
# expandtabs 將tab轉(zhuǎn)換成空格,默認(rèn)一個tab轉(zhuǎn)換成8個空格
# def expandtabs(self, tabsize=None):
# 默認(rèn)情況下tabsize不寫,就是將tab轉(zhuǎn)換成8個空格,如果寫成2,就是轉(zhuǎn)換成2個空格
n = 'zhege shi ceshi zhidaobu cntf'
print(n)
v = n.expandtabs(2)
print(v)
print('==========expandtabs===========')
# find? 尋找子序列位置,如果沒有找到,就返回-1
# def find(self, sub, start=None, end=None):
n = 'ceshi find zhege danci word cntf'
print(n)
# v = n.find('danci')
v = n.find('fuck')
print(v)
print('==========find===========')
# 上述顯示的結(jié)果是17,表示從第一個字符開始從0開始數(shù),第17個字符是符合要求的
# 上述如果沒有找到fuck那么就是顯示-1
# format 字符串格式化,動態(tài)參數(shù)
# def format(*args, **kwargs): # known special case of str.format
# 通過位置來填充字符串
n = 'hello {0} I will fucking the {1}'
print(n)
v = n.format('objk', 'world')
print(v)
print('==========format-位置測試===========')
n1 = 'hello {} I will fucking the {}'
print(n1)
v1 = n1.format('ojbk','world')
print(v1)
print('==========format-不寫位置測試===========')
n2 = 'hello {0} I will fucking the {1} ... and now this is a programe language--- {1}'
print(n2)
v2 = n2.format('ojbk', 'world')
print(v2)
print('==========format-同一個參數(shù)可以填充多次===========')
# 同一個參數(shù)可以填充多次,這個是format的先進(jìn)地方
# 通過key來填充
jbkb = 'world'
name = 'python3'
print('hello, {jbkb}, I will fuck {name}')
# 開始format
print('hello, {jbkb}, I will fuck {name}'.format(jbkb = jbkb, name = name))
print('==========format-通過key來填充===========')
# 通過列表填充
list = ['world', 'python3']
print('hello {names[0]}? I will fuck {names[1]}')
# 開始format
print('hello {names[0]}? I will fuck {names[1]}'.format(names=list))
print('==========format-通過列表來填充===========')
# 通過字典填充
dict = {'obj':'world', 'name':'python3'}
print(dict)
print('hello {names[obj]} I will fucking {names[name]}'.format(names=dict))
print('==========format-通過字典來填充===========')
print('***********format-已經(jīng)測試完畢************')
# index 子序列位置,如果沒有找到,就報錯
# def index(self, sub, start=None, end=None):
n = 'that if fuck the world'
print(n)
v = n.index('fuck')
print(v)
print('==========index測試完畢===========')
# 在字符串中查找fuck的起始位置,這里執(zhí)行結(jié)果是8,表示從左到右從0開始數(shù)起第8個位置
# isalnum 是否是字母和數(shù)字,或者都是字母,或者都是數(shù)字
# def isalnum(self):?
n1 = 'cntf888'
n2 = 'cntf'
n3 = '9988899223'
n4 = 'cntf_fuck998'
print(n1)
print(n2)
print(n3)
# 開始判斷是否是數(shù)字字母或者其中一個是的
v1 = n1.isalnum()
print(v1)
v2 = n2.isalnum()
print(v2)
v3 = n3.isalnum()
print(v3)
v4 = n4.isalnum()
print(v4)
print('==========isalnum測試完畢===========')
# isalpha 是否是字母
# def isalpha(self):
n1 = 'cntf'
n2 = '888666'
print(n1)
v1 = n1.isalpha()
print(v1)
v2 = n2.isalpha()
print(v2)
print('==========isalpha測試完畢===========')
# isdigit 是否是數(shù)字
# def isdigit(self):
n1 = 'cntf'
n2 = '888999'
print(n1)
v1 = n1.isdigit()
print(v1)
v2 = n2.isdigit()
print(v2)
print('==========isdigit測試完畢===========')
# islower 是否是小寫
# def islower(self):
n1 = 'cnft'
n2 = 'CNTf'
print(n1)
print(n2)
v1 = n1.islower()
print(v1)
v2 = n2.islower()
print(v2)
print('==========islower測試完畢===========')
# isspace 是否含有空格
# def isspace(self):
n1 = 'cntf'
n2 = '? '
print(n1)
print(n2)
v1 = n1.isspace()
print(v1)
v2 = n2.isspace()
print(v2)
print('==========isspace測試完畢===========')
# istitle 判斷字符串中所有的單詞拼寫首字母是否為大寫,且其他字母為小寫
# def istitle(self):
n1 = 'This Is String Da Xue'
n2 = 'This is string test shifou shi daxie'
print(n1)
print(n2)
v1 = n1.istitle()
print(v1)
v2 = n2.istitle()
print(v2)
print('==========istitle測試完畢===========')
# isupper 判斷字符串中所有的字母是否是大寫
# def isupper(self):
n1 = 'THIS IS TEST ALL ZIMU SHIFOU SHI DAXUE'
n2 = 'THIS is all zimu no daxue'
print(n1)
print(n2)
v1 = n1.isupper()
print(v1)
v2 = n2.isupper()
print(v2)
print('==========isupper測試完畢===========')
# join 將序列中的元素以指定的字符連接生成一個新的字符串
n1 = 'fuck'
n2 = '---'
print(n1.join(n2))
str = "-"
seq = ("a", "b", "c")
print(str.join(seq))
print('==========join測試完畢===========')
# ljust 內(nèi)容左對齊,右側(cè)填充
# def ljust(self, width, fillchar=None):
n1 = 'cntf that is fucking '
print(n1)
v1 = n1.ljust(30, '2')
print(v1)
print('==========ljust測試完畢===========')
# lower 變成小寫
# def lower(self):
n = 'cntFFUCKingheHe'
print(n)
v = n.lower()
print(v)
print('==========lower測試完畢===========')
# lstrip 移除最側(cè)空白
# def lstrip(self, chars=None):
n = '? ? ? d shiting cntf FUK HEIHEI'
print(n)
v = n.lstrip()
print(v)
print('==========lstrip測試完畢===========')
# partition 分割,前,中,后三部分
# def partition(self, sep):
# 根據(jù)指定的分隔符將字符串進(jìn)行分割
n = 'www.baidu.com'
print(n)
v = n.partition('bai')
print(v)
print('==========partition測試完畢===========')
# 執(zhí)行的結(jié)果就是根據(jù)前,中,后來看
# replace 替換
# def replace(self, old, new, count=None):
n = 'www.baidu.com.net'
print(n)
v = n.replace('www','shit')
print(v)
v1 = n.replace('.','*')
v2 = n.replace('.','*',2)
print(v1)
print(v2)
print('==========replace測試完畢===========')
# old 是指將要被替換的字符串
# new 是指被替換后的新字符串
# max 可選字符串,表示替換不超過多少次
# rfind 返回字符串最后一次出現(xiàn)的位置,如果沒有匹配則返回-1
# def rfind(self, sub, start=None, end=None):
n1 = 'this is ceshi rfingd fucking cntf'
n2 = 'cn'
print(n1)
print(n2)
v1 = n1.rfind('is', 0, 5)
v4 = n1.rfind('is', 5, 0)
v2 = n1.rfind('is')
v3 = n1.find('is')
print(v1)
print(v2)
print(v3)
print(v4)
print('==========rfind測試完畢===========')
# find 是從字符串左邊開始查詢子字符串匹配到的第一個索引
# rfind 默認(rèn)情況下是從字符串右邊開始查詢子字符串匹配到的第一個索引
# 當(dāng)然如果rfind已經(jīng)指定了開始和結(jié)束的位置那么就是根據(jù)指定的位置開始從左至右
# rindex 子字符串最后一次出現(xiàn)在字符串中的索引位置,只不過如果子字符串不在字符串中會報一個異常
# def rindex(self, sub, start=None, end=None):?
n1 = 'this is ceshi rfingd fucking cntf'
print(n1)
v1 = n1.rindex('is')
# v2 = n1.rindex('is', 10)
print(v1)
# print(v2)
print('==========rindex測試完畢===========')
# 默認(rèn)是從左向右開始查找匹配,如果指定了起始位置那么就從起始位置開始查找匹配
# rjust 返回一個原字符串右對齊,并使用空格填充
# def rjust(self, width, fillchar=None):?
n1 = 'this is fucking cntf'
print(n1)
v1 = n1.rjust(30)
v2 = n1.rjust(30, '*')
print(v1)
print(v2)
print('==========rjust測試完畢===========')
# rpartition 從右向左邊開始以前,中,后分割
# def rpartition(self, sep):?
n = 'www.baidu.com.net'
print(n)
v = n.rpartition("du")
print(v)
print('==========rpartition測試完畢===========')
# rsplit 從左到右開始指定分隔符對字符串進(jìn)行分割并返回一個列表,默認(rèn)分隔符為所有的空字符,包括空格,換行(\n),制表符(\t)
# def rsplit(self, sep=None, maxsplit=None):?
# sep 可選參數(shù),指定分隔符,默認(rèn)為所有的空字符,包括換行符和制表符
# count 可選參數(shù),分割次數(shù),默認(rèn)為分隔符在字符串中出現(xiàn)的總次數(shù)
n1 = 'this is ceshi rsplit fucking cntf'
print(n1)
v1 = n1.rsplit()
print(v1)
v2 = n1.rsplit('i')
print(v2)
v3 = n1.rsplit('i', 1)
print(v3)
print('==========rsplit測試完畢===========')
# rstrip 返回刪除字符串末尾指定的字符
# def rstrip(self, chars=None):?
n1 = " ***********this fucking cntf************"
n2 = ' ***********this fucking cntf************? '
print(n1)
v1 = n1.rstrip()
print(v1)
v2 = n2.rstrip('*')
print(v2)
print('==========rstrip測試完畢===========')
# split 分割, maxsplit 最多分割幾次
# def split(self, sep=None, maxsplit=None):?
n1 = 'this is ceshi fenge fucking cntf'
print(n1)
v1 = n1.split()
print(v1)
v2 = n1.split('e')
print(v2)
v3 = n1.split('e', 1)
print(v3)
# 真的字符串進(jìn)行分割,默認(rèn)是以空白符,空格進(jìn)行分割
# sep 是指定以什么作為分割符
# maxsplit 指定最大分割幾次
print('==========split測試完畢===========')
# splitlines 根據(jù)換行分割
# def splitlines(self, keepends=False):?
n1 = 'this is ceshi fenge fucking cntf'
print(n1)
v1 = n1.splitlines()
print(v1)
print('==========splitlines測試完畢===========')
# startswith 檢查字符串是否是以指定的子字符開頭,如果是返回True,如果不是返回False
# 如果參數(shù)beg和end指定值,則在指定范圍內(nèi)檢查
# def startswith(self, prefix, start=None, end=None):?
n = 'this is test start swith hehe example wowow? ok ...!!!'
print(n)
v = n.startswith('this')
print(v)
v1 = n.startswith('is')
print(v1)
v2 = n.startswith('test', 8)
print(v2)
print('==========startswith測試完畢===========')
# strip 移除兩段空白,或者指定的字符串
# def strip(self, chars=None):
n1 = '00000000009988cntf-shiting889900000000000000'
print(n1)
v1 = n1.strip()
print(v1)
# 默認(rèn)不指定就是空白符
v2 = n1.strip('0')
print(v2)
n2 = '? ? ? ? shiting and fucking cntf? ? ? ? ? '
print(n2)
v4 = n2.strip()
print(v4)
print('==========strip測試完畢===========')
# swapcase 大寫變小寫,小寫變大寫
# def swapcase(self):
n1 = 'CNTF'
n2 = 'cntf'
n3 = 'CNtf'
print(n1)
print(n2)
print(n3)
v1 = n1.swapcase()
print(v1)
v2 = n2.swapcase()
print(v2)
v3 = n3.swapcase()
print(v3)
print('==========swapcase測試完畢===========')
# title 所有單詞都是以大寫開頭
# def title(self):
n = 'this is test the titles cntf'
print(n)?
v = n.title()
print(v)
print('==========title測試完畢===========')
# translate 需要轉(zhuǎn)換的字符
# 轉(zhuǎn)換,需要先做一個對應(yīng)表,最后一個表示刪除字符集合
# def translate(self, table, deletechars=None):?
# from string import maketrans? # Required to call maketrans function.
# intab = "aeiou"
# outtab = "12345"
# trantab = maketrans(intab, outtab)
# str = "this is string example....wow!!!";
# print str.translate(trantab, 'xm');
# print('==========translate測試完畢===========')
# upper 將小寫字母變成大寫
# def upper(self):
n = 'fucking'
print(n)
v = n.upper()
print(v)
print('==========upper測試完畢===========')
# zfill 方法返回指定長度的字符串,原字符串右對齊,前面填充0
# def zfill(self, width):
n1 = 'shit'
print(n1)
v1 = n1.zfill(60)
print(v1)
print('==========zfill測試完畢===========')
# expandtabs 根據(jù)制表符進(jìn)行匹配
test = '1234567\t89'
print(test)
v = test.expandtabs(6)
print(v,len(v))
# expandtabs(6)表示針對字符每6個字符為一組,如果找到每組中含有制表符\t
# 那么算出這個有制表符的組中除了對于的字符之外的位置用空格填充
# 實際用途中可以制作表格
# isdecimal isdigit
# 判斷是否為數(shù)字,isdigit牛逼點 isnumeric更牛逼
n = "⑥"
# v1 = n.isdecimal()
v2 = n.isdigit()
v3 = n.isnumeric()
print(v1,v2,v3)
# Python3正常執(zhí)行
# isidentifier()
# a = "def"
# v = a.isidentifier()
# print(v)
# isprintable 是否可以打印 例如\n \t 是不可以打印的
# 需要記住經(jīng)常使用的 join split find strip upper lower replace
test = input(">>>")
for item in range(0, len(test)):
print(item, test[item])