Python基础知识点总结:【数字功能】【字符串功能】

数字(int)常见功能

在网络课上,老师把这些功能称为神奇的魔法,想要揭开魔法神奇的面纱,想要看看有什么招数,在Pycharm里敲击三个字母(int),按住Ctrl,将鼠标点在int上,这时鼠标会变成小手。点进去,有关于int所有魔法的介绍(这个时候就是考验你英语水平的时候了呢,加油)

功能 描述 举例

int

#将字符串转化为十进制数字

#base:按照base的进制类型进行转化,可省略,默认为按照十进制数的形式转换

a = '123'
print(type(a),a)
b = int (a)
print(type(b),b)

运行结果

<class 'str'> 123

  <class 'int'> 123

 

 bit_length

 

#当前数字的二进制,至少要用几位来表示

 
num = 8
test = num.bit_length()
print(test)

字符串(str)常见功能

今天的重点就是总结字符串的这一系列魔法及其使用方法。

功能

描述

举例

capitalize

#Return a capitalized version of the string.

#首字母大写

test = "xhg"
v = test.capitalize()
print(v)
 

casefold

  

#这两个功能都是将字符串中的所有大写换成小写

#lower功能更为强大,可以将特殊未知字符变成相对应的小写

  
test = "XiaoHuoguo"
v1 = test.casefold()
v2 = test.lower()
print(v1,v2)
 

lower

 

center

 

#设置宽度,并将内容居中
#第一个参数代表总长度,第二个参数代表空白位置的填充,为一个字符,可省略

 
test = "love"
v = test.center(20,'X')
print(v)
 

count

 

# def count(self, sub, start=None, end=None):
# S.count(sub[, start[, end]]) -> int

#去目标字符串中寻找子序列出现的次数。第二三个参数为目标字符串中开始和结束寻找的位置,可省略
test 

 
test = 'ajhklsdghexhgldkxhg'
v = test.count('xhg',14,18)
print(v)
 

startswith

  

#def startswith(self, prefix, start=None, end=None):
#S.startswith(prefix[, start[, end]]) -> bool
#Return True if S starts with the specified prefix, False otherwise.
#With optional start, test S beginning at that position.
#With optional end, stop comparing S at that position.
#prefix can also be a tuple of strings to try.

#检测目标字符串中以XX结尾、以XX开始,XX可以是一个字符,也可以是一个子序列

#第二三个参数可以指定判断开始的字符和结束的字符(左闭右开)


 

  
test = 'xhgxhg'
v1 = test.startswith('xh')
v2 = test.endswith('g',2,5)
print(v1,v2)
 

endswith

 

find

  

# def find(self, sub, start=None, end=None):

#S.find(sub[, start[, end]]) -> int
#Return the lowest index in S where substring sub is found,Return -1 on failure.

上述是关于find的定义

这两个功能都能实现从开始往后找,找到第一个之后,并获取其位置

但是如果find没找到返回-1,而index没找到,则会报错。因此我们通常选择find来实现此功能。

  
test = 'xhgxhaeifgsdlk'
v1 = test.find('g',3,10)
v2 = test.index('a')
print(v1,v2)
 

index

 

format

  

二者都可以实现格式化,将一个字符串中的占位符替换为指定的值的功能。只是二者替换形式有所不同,通过例子来感受一下。

需要强调的是:

在format_map,替换内容加{},否则会报错。

在format中,有两种替换方式,一种是字符串替换,一种是位置替换。

  
test1 = 'i am {name}, age {num}'
test2 = 'i am {0}, age {1}'
v1 = test1
v2 = test1.format(name='xhg',num=20)
v3 = test2.format('xhg',20)
v4 = test1.format_map({"name": 'xhg','num':20})
print(v1,v2,v3,v4)
 

format_map

 

isalnum

#Return True if the string is an alpha-numeric string, False otherwise.

#there is at least one character in the string.

#判断目标字符串中是否只包含字母和数字

 
test = 'dsflj2'
v = test.isalnum()
print(v)
 

expandtabs

 #Return a copy where all tab characters are expanded using spaces.

# If tabsize is not given, a tab size of 8 characters is assumed.

#这个功能可以用来制作表格

 
test1 = 'username\tage\tpassword\nxhg\t20\t888888\n'
test2 ='slj\ta\tfj\t'
v1 = test1.expandtabs(20)
v2 = test2.expandtabs(1)
v3 = test2.expandtabs(4)
print(v1,v2,v3)
 

isdecimal

#这三个功能类似,都是检测字符串中是否全为数字。

#不同的是:

isdecimal检测普通数字,如1 2 3...

isdigit还可检测特殊数字,如①....

isnumeric除了上述两种数字外,可以检测汉字数字。如 二、 三 ...

   
test1= "1"
test2= "1①"
test3= "1①一"
v1 = test1.isdecimal()
v2 = test2.isdigit()
v3 = test3.isnumeric()
print(v1,v2,v3)
 

isdigit

 

isnumeric

 

isidentifier

 #检测目标字符串是否为标识符

#Return True if the string is a valid Python identifier, False otherwise.

 
test = 'defad3_'
v = test.isidentifier()
print(v)
 

isprintable

 

 #检测是否存在不可显示的字符   \t制表符  \n换行符

#Return True if the string is printable, False otherwise.

 
test = 'fsd\nklfusdf'
v =test.isprintable()
print(v)
 

isspace

 

#判断是否全部是空格

需要强调的是:判断的目标字符串中至少要有一个字符。如果没有字符的话返回也是False

 
test = '   '
v = test.isspace()
print(v)

istitle

 

#istitle判断是否是标题。

#title是将目标序列转换为标题格式

#所谓标题格式就是每个单词首字母为大写

test = 'Process finished with exit code'
v1 = test.istitle()
v2 = test.title()
v3 = v2.istitle()
print(v1,v2,v3)

title

join

#将字符串中的每一个元素按照指定分割符进行拼接

#Concatenate any number of strings.(分割符可以是任意个,也可以是空格键)

 
test = '深度学习与计算机视觉'
v = '**'.join(test)
print(test,v)
 

center

 

#这三个功能都可以设置宽度

#不同的是center将内容居中(两边填充);ljust是内容放在左边(右边填充),rjust是内容放在右边(左边填充)

#第一个参数代指新字符串的长度,第二个参数为一个字符,空白位置填充,可有可无

  
   
test = 'love'
v1 =test.center(20,'*')
v2 = test.ljust(20,'*')
v3 = test.rjust(20,'*')
print(v1,v2,v3)
 

ljust

rjust

 

islower

#islower  判断字符串中字符是否全部为小写

#isupper 判断字符串中字符是否全部为大写

#lower   将目标字符串中的所有大写全转换为小写

#upper   将目标字符串中的所有小写全转换为大写

(这几个功能最大的意义在于我们平时输入验证码时,大小写都可以识别的原因就是在后台进行了这样一个转换)

  
test1 = 'XhG'
v1 = test1.islower()
v2 = test1.lower()
v3 = test1.isupper()
v4 = test1.upper()
print(v1,v2,v3,v4)
 

lower

isupper

upper

strip

#取出左右空白;

#去除\n,\t;

#移除指定字符串(有限最多匹配原则进行去除)

test = 'love '
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(test,v1,v2,v3)

maketrans

#这两个功能通常是连在一起用的

#maketrans是将指定字符串进行一个替换(替换是一一对应替换如将1--》a,字符串中所有1都将替换为a)

# translate:Replace each character in the string using the given translation table.(英文翻译更为准确)

  
s = 'xhg Love HG'
m = str.maketrans('xhg', '小伙郭')
new_s = s.translate(m)
print(new_s)

translate

 

partition

#三个功能都可以实现将字符串按照指定字符进行分割

#不同的是:

partition分割为3份,从左边开始找分割字符。

rpartition分割为3份,从右边开始找分割字符。

关于上述两个功能返回结果表述:

returns a 3-tuple containing the part before the separator, the separator itself, and the part after it.If the separator is not found, returns a 3-tuple containing the original string and two empty strings.)

split可以按照制定分割字符进行全部分割,但是分割后分隔符丢失。(也可以根据第二个参数制定分割份数)

   
test = 'dhsfesqsg'
v1 = test.partition('p')
v2 = test.rpartition('p')
v3 = test.split('p',4)
print(v1,v2,v3)
 

rpartition

 

split

 

splitlines

 

#根据换行符来进行分割

#如果结果中想要保留换行符,参数为True,否则为False

 
test = 'fah\nadsgak\njfh\nakjfdfh\n'
v = test.splitlines(True)
print(v)

swapcase

#将目标字符串中的大写换成小写,将小写换成大写

test = 'AAaa'
v = test.swapcase()
print(v)

replace

#替换,不同maketrans(将指定字符串进行一个替换,一个字节对弈一个字节),replace的替换是可以任意进行,字符个数不要要一一对应,我一字字符可以替换多个字符,多个字符也可以用一个来替换。

#第三个参数可以指定替换的个数

test = 'lovelovelovelove'
v1 = test.replace('lo','*')
v2 = test.replace('o','*',3)
print(v1,v2)
str.[]

#索引,下标,获取字符串中的某一个字符

test = 'lovel'
v = test[3]
print(v)
str.[m:n]

#切片,获取字符串中第m个到第N的字符(不包括第N个,左闭右开) 

需要注意的是,当N取-1时候,代表截取字符串从当前位置到最后一位

test = 'lovel'
v = test[0:3]
print(v)

     range

#帮助创建连续的数字,第三个参数通过设置不常来指定不连续

需要注意的是:

Python2.7(直接创建)和python3(通过for循环来创建,节省内存空间)

 
v = range(0 ,100 ,20)
print(v)
for m in v:
    print(m)

 字符串功能小结

字符串的功能太多了,我们需要做的呢,就是会用。但是以下几个是要牢牢记住的。(8个)

jion      split        find       strip  upper  lower  replace  range

还有四个操作呢,非常重要。几乎在所有数据类型中都可以使用的

len()  for循环  索引  切片

这些需要记住的,我在表格里用黄色加粗标注。

注意啦:字符串一旦创建,便不可修改。一旦修改或者拼接,都会造成重新生成一个新的字符串

(其实这些功能还需要多练习去体会它的用法,很多时候看英文的注释会更深刻的理解哦)

猜你喜欢

转载自blog.csdn.net/qq_40925239/article/details/83591211