当前位置:网站首页>basic operator

basic operator

2022-08-09 09:58:00 Wu Xizhong

一.格式化字符串的方法

1.字符串的拼接

方法:str + str
代码演示:

name = 'jhon'
print(name + ',Nice to meet you')

输出打印结果:

jhon,Nice to meet you

2.参数传递

方法:str,str
代码演示:

s = 18
print('my age is:',s)

输出打印结果:

my age is : 18

3.占位符

方法:1.字符串(str)类型用%s占位
2.浮点(float)类型用%f占位
3.整型(int)类型用%d 占位

代码演示:

name = 'AMY'
age = 18
height = 1.75
print('%s的年龄%d岁,身高%f米'%(name,age,height))

输出打印结果:

AMY的年龄18岁,身高1.75

4.字符串格式化方式

1.方法:f ’ ’

代码演示:

name = 'Amy'
age = 18
height = '1.75'
print(f'{name}的年龄{age}岁,身高{height}米')

输出打印结果:

```python
Amy的年龄18岁,身高1.75

2.方法:str.format()

代码演示:

print('{}的年龄{}岁,身高{}米'.format('amy',18,1.75))

输出打印结果:

amy的年龄18岁,身高1.75

二.Common functions for string manipulation

1.求字符串的长度

方法:len()

代码演示:

message = 'hello world'
print(len(message))

输出打印结果:

11

2.Find the corresponding in the stringASCII码最大的那个字符

方法:max()

代码演示:

name = 'Jhong'
print(max(name))

输出打印结果:

'o'

3.Find the corresponding in the stringASCIIThe character with the smallest code

方法:min()
代码演示

name = 'Jhong'
print(min(name))

输出打印结果:

'g'

4.Split the string and return the value as a list

方法:str.split(‘Which way to split’)
代码演示:Split the string by spaces and return a list

message 'hello world'
a = message.split(' ')
print(a)

输出打印结果:

['hello', 'world']

5.Split the string and return the value as a string

方法:‘按什么方式分割’.join(可迭代对象) 备注:The elements in the iterable must be strings to be concatenated

代码演示:

a = 'Nice'
print('-'.join(a))

输出打印结果:

'N-i-c-e'

6.Convert the characters in the original string to all uppercase

方法:str.upper()
代码演示:

name = 'arry'
print(name.upper())

输出打印结果:

'ARRY'

7.Convert the characters in the original string to all lowercase

方法:str.lower()
代码演示:

name = 'AmY'
print(name.lower())

输出打印结果:

'amy'

8.Convert the first letter in the original string to uppercase

方法:str.title()
代码演示:

name = 'amy'
print(name.title())

输出打印结果:

'Amy'

9.Determines whether a string letter is pure uppercase or pure lowercase

1.方法:str.isupper()--------判断是否全部大写,返回值为布尔值
代码演示:

message = 'hello'
message1 = 'Hello'
message2 = 'HELLO'
print(message.isupper)
print(message1.isupper)
print(message2.isupper)

输出打印结果:

False
False
True

2.方法:str.islower()---------判断是否全部小写,返回值为布尔值
代码演示:

message = 'hello'
message1 = 'Hello'
message2 = 'HELLO'
print(message.islower)
print(message1.islower)
print(message2.islower)

输出打印结果:

True
False
False

10.字符串去空格

1.str.strip():去除左右两边空格
2.str.lstrip():去除左边空格
3.str.rstrip():去除右边空格

二.运算符

1.算术运算符

Arithmetic operators follow the normal mathematical operations of addition, subtraction, multiplication and division
The representation of arithmetic operators:+(加法运算符)、-(减法运算符)、*(乘法运算符)、/(除法运算符,结果为浮点类型) 、//(整除,Returns the calculated integer bits,always returns an integer)、%(取模,求两个数相除的余数)、**(求一个值的几次幂)
1.The addition operation between integers is a summation
例如:

print(5+3)

打印输出结果:

8

2.The addition operation between strings is concatenation
例如:

print('a'+'b')

打印输出结果:

ab

3.Strings and strings cannot be multiplied,But strings and integers can be multiplied
例如:

print('a'*2)

输出打印结果:

'aa'

4.Subtraction cannot be performed between strings、除法、乘法、幂次方运算

2.赋值运算符

符号=表示赋值

a = 2

The above code means will2赋值给变量a

3.比较运算符

符号:== >= <= !=
例子:

a == 2

以上代码表示a的值与2 相等

a >= 2

以上代码表示a的值大于或等于2

a <= 2

以上代码表示a的值小于或等于2

a != 2

以上代码表示a的值不等于2

4.身份运算符

符号:is,Usually used to determine whether it is the same object
例子:

a is b

以上代码表示a是b

5.成员运算符

符号:in , not in
例子:

a in b
a not in b

6.逻辑运算符

1.逻辑与(and)
2.逻辑非(not)
3.逻辑或(or)
注意:1.In the logical NOT operation, if a non-boolean value type participates in the logical operation, it will be automatically converted into a boolean value before performing the logical operation
2.Boolean value for non-zero non-null dataTrue

代码演示:

print(11 and 12 )
print(not 13)
print(11 or 12)

打印输出结果:

12
False
11
原网站

版权声明
本文为[Wu Xizhong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090947196217.html