python入门函数讲解【中】(简单明了,一分钟掌握一个)

🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝

🥰 博客首页:knighthood2001

😗 欢迎点赞👍评论🗨️

❤️ 热爱python,期待与大家一同进步成长!!❤️

👀给大家推荐一款很火爆的刷题、面试求职网站👀

跟我一起来巩固基础和刷题吧

        今天继续讲解牛客网已经给我们整理好的非常详细的python函数,有些非常重要哦,大家可以跟着我一起学习哦。给大家推荐一款很火爆的刷题、面试求职网站--牛客https://www.nowcoder.com/link/pc_csdncpt_knight_python

目录

count()

描述

参数

返回值

bytes.decode()

描述

参数

返回值

find()

描述

参数

返回值

index()

描述

参数

返回值

List index()

描述

参数

返回值

join()

描述

参数

返回值

len()

描述

参数

返回值

lstrip()

描述

参数

返回值

max()

描述

参数

返回值

List max()方法

描述

参数

返回值

replace()

描述

参数

返回值

strip()

描述

参数

返回值

list()

描述

参数

返回值

List append()

描述

返回值


count()

描述

        count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

count()方法语法:

1
str.count(sub, start= 0,end=len(string))

参数

  • sub -- 搜索的子字符串
  • start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
  • end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

返回值

该方法返回子字符串在字符串中出现的次数。


以下实例展示了count()方法的实例:

str="www.nowcoder.com"
sub='o'
print ("str.count('o') : ", str.count(sub))
 
sub='now'
print ("str.count('now', 0, 10) : ", str.count(sub,0,10))

结果如下:

str.count('o') :  3
str.count('now', 0, 10) :  1

bytes.decode()

描述

        decode() 方法以指定的编码格式解码 bytes 对象。默认编码为 'utf-8'。

decode()方法语法:

1

bytes.decode(encoding="utf-8", errors="strict")

参数

  • encoding -- 要使用的编码,如"UTF-8"。
  • errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

返回值

该方法返回解码后的字符串。


以下实例展示了decode()方法的实例:

str = "牛客教程";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")

print(str)

print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)

print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))

结果如下:

牛客教程
UTF-8 编码: b'\xe7\x89\x9b\xe5\xae\xa2\xe6\x95\x99\xe7\xa8\x8b'
GBK 编码: b'\xc5\xa3\xbf\xcd\xbd\xcc\xb3\xcc'
UTF-8 解码: 牛客教程
GBK 解码: 牛客教程

以上可以看出编码的重要性。 

find()

描述

        find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。

find()方法语法:

1

str.find(str, beg=0, end=len(string))

参数

  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0。
  • end -- 结束索引,默认为字符串的长度。

返回值

如果包含子字符串返回开始的索引值,否则返回-1。


以下实例展示了find()方法的实例:

str1 = "Nowcoder example....wow!!!"
str2 = "exam";

print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))

结果如下:

9
9
-1

index()

描述

        index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

index()方法语法:

1

str.index(str, beg=0, end=len(string))

参数

  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0。
  • end -- 结束索引,默认为字符串的长度。

返回值

如果包含子字符串返回开始的索引值,否则抛出异常。


以下实例展示了index()方法的实例:

str1 = "Nowcoder example....wow!!!"
str2 = "exam";

print (str1.index(str2))
print (str1.index(str2, 5))
print (str1.index(str2, 10))

结果如下:

Traceback (most recent call last):
  File "C:/work/P_workspace/cainiao/cainiao/customize/demo.py", line 6, in <module>
    print (str1.index(str2, 10))
ValueError: substring not found
9
9

index()函数还是比较重要的,可以了解下。

List index()

描述

        index() 函数用于从列表中找出某个值第一个匹配项的索引位置。

index()方法语法:

1

list.index(x[, start[, end]])

参数

  • x-- 查找的对象。
  • start-- 可选,查找的起始位置。
  • end-- 可选,查找的结束位置。

返回值

该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。


以下实例展示了 index()函数的使用方法:

list1 = ['Google', 'Nowcoder', 'Taobao']
print ('Nowcoder 索引值为', list1.index('Nowcoder'))
print ('Taobao 索引值为', list1.index('Taobao'))
Nowcoder 索引值为 1
Taobao 索引值为 2

join()

描述

        Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

join()方法语法:

1

str.join(sequence)

参数

  • sequence -- 要连接的元素序列。

返回值

返回通过指定字符连接序列中元素后生成的新字符串。


以下实例展示了join()的使用方法:

s1 = "-"
s2 = ""
seq = ("n", "o", "w", "c", "o", "d", "e", "r") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))

结果如下: 

n-o-w-c-o-d-e-r
nowcoder

这个函数挺重要的,平时用的非常多。

len()

描述

        Python len() 方法返回对象(字符、列表、元组等)长度或项目个数。

len()方法语法:

1

len( s )

参数

  • s -- 对象。

返回值

返回对象长度。


以下实例展示了 len() 的使用方法:

>>>str = "nowcoder"
>>> len(str)             # 字符串长度
8
>>> l = [1,2,3,4,5]
>>> len(l)               # 列表元素个数
5

lstrip()

描述

        lstrip() 方法用于截掉字符串左边的空格或指定字符。

lstrip()方法语法:

1
str.lstrip([chars])

参数

  • chars --指定截取的字符。

返回值

返回截掉字符串左边的空格或指定字符后生成的新字符串。


以下实例展示了lstrip()的使用方法:

str = "     this is string example....wow!!!     ";
print( str.lstrip() );
str = "88888888this is string example....wow!!!8888888";
print( str.lstrip('8') );

结果如下:

this is string example....wow!!!    
this is string example....wow!!!8888888

max()

描述

        max() 方法返回字符串中最大的字母。

max()方法语法:

1

max(str)

参数

  • str -- 字符串。

返回值

返回字符串中最大的字母。


以下实例展示了max()函数的使用方法:

str = "nowcoder"
print ("最大字符: " + max(str))

结果如下: 

最大字符: w

List max()方法

描述

        max() 方法返回列表元素中的最大值。

max()方法语法:

1

max(list)

参数

  • list -- 要返回最大值的列表。

返回值

返回列表元素中的最大值。


以下实例展示了 max()函数的使用方法:

list1, list2 = ['Google', 'Nowcoder', 'Taobao'], [456, 700, 200]

print ("list1 最大元素值 : ", max(list1))
print ("list2 最大元素值 : ", max(list2))
list1 最大元素值 :  Taobao
list2 最大元素值 :  700

replace()

描述

        replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

replace()方法语法:

1
str.replace(old, new[, max])

参数

  • old -- 将被替换的子字符串。
  • new -- 新字符串,用于替换old子字符串。
  • max -- 可选字符串, 替换不超过 max 次

返回值

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。


以下实例展示了replace()函数的使用方法:

str = "www.niuke.com"
print ("牛客教程旧地址:", str)
print ("牛客教程新地址:", str.replace("niuke", "nowcoder"))
 
str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))

结果如下:

牛客教程旧地址: www.niuke.com
牛客教程新地址: www.nowcoder.com
thwas was string example....wow!!!

strip()

描述

        Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

strip()方法语法:

1

str.strip([chars]);

参数

  • chars -- 移除字符串头尾指定的字符序列。

返回值

返回移除字符串头尾指定的字符序列生成的新字符串。


以下实例展示了 strip() 函数的使用方法:

str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' ))  # 指定字符串 *
this is **string** example....wow!!!

从结果上看,可以注意到中间部分的字符并未删除。

str = "123abcnowcoder321"
print (str.strip( '12' ))  # 字符序列为 12
3abcnowcoder3

list()

描述

        list() 方法用于将元组或字符串转换为列表。

注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中。

list()方法语法:

1

list( seq )

参数

  • seq -- 要转换为列表的元组或字符串。

返回值

返回列表。


以下实例展示了 list()函数的使用方法:

aTuple = (123, 'Google', 'Nowcoder', 'Taobao')
list1 = list(aTuple)
print ("列表元素 : ", list1)

str="Hello World"
list2=list(str)
print ("列表元素 : ", list2)

结果如下:

列表元素 :  [123, 'Google', 'Nowcoder', 'Taobao']
列表元素 :  ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

List append()

描述

        append() 方法用于在列表末尾添加新的对象。

append()方法语法:

1

list.append(obj)

  • obj -- 添加到列表末尾的对象。

返回值

该方法无返回值,但是会修改原来的列表。


以下实例展示了 append()函数的使用方法:

list1 = ['Google', 'Nowcoder', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)
更新后的列表 :  ['Google', 'Nowcoder', 'Taobao', 'Baidu']

  • 74
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 70
    评论
评论 70
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

knighthood2001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值