大连专业企业建站找哪家,做平面设计常用的网站,江苏网站建设企业,案列网站6.字符串
6.1 字符串的表示方式
6.1.1 普通字符串
普通字符串指用单引号()或双引号(”)括起来的字符串。例如:Hello或Hello Hello
HelloHello
Hellos\u0048\u0065\u006c\u006c\u006f…6.字符串
6.1 字符串的表示方式
6.1.1 普通字符串
普通字符串指用单引号()或双引号(”)括起来的字符串。例如:Hello或Hello Hello
HelloHello
Hellos\u0048\u0065\u006c\u006c\u006fs
HelloHelloworld
HelloworldHelloworld
Helloworld
6.1.2 原始字符串
在 Python 中以字母 r 或者 R 作为前缀的字符串例如 r...和R...被称为原始字符串。 shello\tworld\nPython\t3print(s)
hello world
Python 3srhello\tworld\nPython\t3print(s)
hello\tworld\nPython\t3
6.1.3 长字符串
使用三个单引号()或三个双引号()括起来. sxxxbfdzbddfbddfbds
\n xxx\n bfdzbd\n dfbd\n dfbd\n print(s)xxxbfdzbddfbddfbd
6.2 字符串与数字相互转换
6.2.1 将字符串转换为数字 int(10)
10int(10.2) #类型不同不能转换
Traceback (most recent call last):File pyshell, line 1, in module
ValueError: invalid literal for int() with base 10: 10.2float(10.2)
10.2int(ab) #类型不同不能转换
Traceback (most recent call last):File pyshell, line 1, in module
ValueError: invalid literal for int() with base 10: abint(ab,16) #按十六进制进行转换
171
6.2.2 将数字转换为字符串 str(12)
12str(12.3)
12.3str(True)
True
6.3 格式化字符串
Python的字符串可通过占位符%、format ()方法和f-strings三种方式实现格式化输出。
6.3.1 占位符% namexiaoqiannihao,wojiao%s % name
nihao,wojiaoxiaoqianage20nihao,wojiao%s,今年%d岁%(name,age)
nihao,wojiaoxiaoqian,今年20岁
6.3.2 format()方法
字符串.format (参数列表) name小强age20你好我叫{}今年我{}岁了。.format(name,age)
你好我叫小强今年我20岁了。你好我叫{1}今年我{0}岁了。.format(age,name) #可以指定参数序号顺序
你好我叫小强今年我20岁了。
format (方法还可以对数字进行格式化包括保留n位小数、数字补齐和显示百分比。 ①保留 n位小数。格式为“{:.nf}” ②数字补齐。格式为“{:mnd]”其中m 表示补齐的数字n 表示补齐后数字的长度表示在原数字左侧进行补充。 ③显示百分比。可以将数字以百分比形式显示。其格式为“{:.n%)”其中n 表示保留的小数位。 pi3.1415{:.2f}.format(pi)
3.14num1{:03d}.format(num)
001num
1num0.1{:.0%}.format(num)
10%
6.3.3 f-strings addr广州f欢迎来到{addr}
欢迎来到广州name小强age20gender男f我的名字是{name}今年{age}岁性别{gender}。
我的名字是小强今年20岁性别男。
6.4 操作字符串
6.4.1 字符串拼接 str_one人生苦短str_two我用python。str_onestr_two
人生苦短我用python。str_three12str_onestr_three #类型要一样
Traceback (most recent call last):File pyshell, line 1, in module
TypeError: can only concatenate str (not int) to strstr_onestr(str_three)
人生苦短12
6.4.2 字符串查找
str.find(sub[,start[,end]])查找子字符串在索引start到end之间查找子字符串sub如果找到则返回最左端位置的索引;如果没有找到则返回-1。 6.4.3 字符串替换
str.replace(old,new, countNone)字符串替换new子字符串替换old子字符串。count参数指定了替换old子字符串的个数count被省略则替换所有old子字符串。 word我是小强我今年20岁。word.replace(我,他)
他是小强他今年20岁。word.replace(我,他,1)
他是小强我今年20岁。word.replace(他,你)
我是小强我今年20岁。 6.4.4 字符串分割
str.splite(sepNone,maxsplit-1)使用sep子字符串分割字符串str,返回一个分割以后的字符串列表。maxsplit是最大分割次数如果maxsplit被省略则表示不限制分割次数。 word1 2 3 4 5word.split()
[1, 2, 3, 4, 5]worda,b,c,d,eword.split(,)
[a, b, c, d, e]word.split(,,3)
[a, b, c, d,e] 6.4.5 去除字符串两侧空格
str.strip(charsNone)一般用于去除字符串两侧的空格参数chars用于设置要去除的字符默认要去除的字符为空格 word strip word.strip()
stripword**strip*word.strip(*)
strip 7.函数
7.1 定义函数
def 函数名([参数列表]): [函数文档字符串] 函数体 [return 语句] def rect_area():print(* * 13)print(长方形面积%d%(12*2))print(* * 13) rect_area #函数地址
function rect_area at 0x0391C270rect_area() #函数调用
*************
长方形面积24
*************def rect_area(width,heigth):areawidth*heigthreturn area print(rect_area(10,2))
20 7.2 调用函数
函数名([参数列表]) def rect_area():print(* * 13)print(长方形面积%d%(12*2))print(* * 13) rect_area #函数地址
function rect_area at 0x0391C270rect_area() #函数调用
*************
长方形面积24
*************
7.3 函数参数传递
7.3.1 位置参数 def division(num_one,num_two):print(num_one/num_two) division(6,2) #传递的参数与定义的参数一一对应
3.0
7.3.2 关键字参数 def division(num_one,num_two):print(num_one/num_two) division(num_one10,num_two2)
5.0 7.3.3 默认参数
默认的参数要放到参数列表的最后面否则会报错 def info(name,age,sex男):print(f姓名:{name})print(f年龄:{age})print(f性别:{sex}) info(namexiao,age20)
姓名:xiao
年龄:20
性别:男info(xiao,20)
姓名:xiao
年龄:20
性别:男info(namexiao,age20,sex女)
姓名:xiao
年龄:20
性别:女def info(name,sex男,age):print(f姓名:{name})print(f年龄:{age})print(f性别:{sex})File pyshell, line 1
SyntaxError: non-default argument follows default argument 7.3.4 不定长参数
def 函数名([formal_args,] *args **kwargs): #两个参数可以同时存在 [函数文档字符串] 函数体 [return 语句]
①*args接收的参数按原始方式保存 def test(*args):print(args) test(1,2,3,a,b)
(1, 2, 3, a, b)
②**kwargs接收的参数按字段键值对方式保存 def test(**kwargs):print(kwargs) test(a1,b2,c3,da,eb)
{a: 1, b: 2, c: 3, d: a, e: b} 7.4 变量作用域
7.4.1 局部变量 def use_var():namepythonprint(name) use_var()
pythonprint(name) #name是局部变量作用在use_var函数内
Traceback (most recent call last):File pyshell, line 1, in module
NameError: name name is not defined7.4.2 全局变量 count10 #定义一个全局变量def use_var():print(count) use_var()
10def use_var():global count #声明count是全局变量不声明会报错count10print(count) use_var()
20def use_var():count10print(count) use_var()
Traceback (most recent call last):File pyshell, line 1, in moduleFile pyshell, line 2, in use_var
UnboundLocalError: local variable count referenced before assignmentprint(count)
20 7.5 函数特殊形式
7.5.1 匿名函数
匿名函数是一种不需要命名的函数。它通常用于简单的函数操作可以在需要时直接定义和使用而不需要为其分配一个特定的名称。匿名函数在很多编程语言中都存在例如Python、JavaScript和Ruby等。在Python中使用lambda关键字来定义匿名函数。
lambda [argl [,arg2,...argn]]:expression
[argl [,arg2,...argn]]:参数列表
expression单一的函数表达式用于实现简单的功能 arealambda a,h:(a*h)*0.5print(area)
function lambda at 0x0380EBB8print(area(3,4))
6.0 7.5.2 递归函数
递归函数是指在函数的定义中使用函数自身的一种编程技巧。简单来说递归函数是通过调用自身来解决问题的函数。 def factorial(num):if num1:return 1else:return num*factorial(num-1) factorial(5)
120 7.6 常用内置函数
abs(): 计算绝对值其参数必须是数字类型。 len(): 返回序列对象 (字符串、列表、元组等) 的长度。 map(): 根据提供的函数对指定的序列做映射。 help(): 用于查看函数或模块的使用说明。 ord(): 用于返回 Unicode 字符对应的码值。 chr():与 ord0功能相反用于返回码值对应的 Unicode 字符。 filter(): 用于过滤序列返回由符合条件的元素组成的新列表 abs(-5)
5abs(3.14)
3.14len(python)
6ord(A)
65chr(65)
Ahelp(len)
Help on built-in function len in module builtins:len(obj, /)Return the number of items in a container.