标签:star 数组 abc sign upper dig ber 分割 必须
print(type(123))
print(type(4545.666))
print(type(1323.))
print(type(‘adbn‘))
<class ‘int‘>
<class ‘float‘>
<class ‘float‘>
<class ‘str‘>
print(type([1,2,2,3,4,5,56,‘a‘,‘b‘]))
print(type((1,‘xddfsdfd‘)))
print(type(set([‘s‘,‘rff‘,‘dd‘])))
print(type({‘a‘: 1, ‘b‘: 2}))
<class ‘list‘>
<class ‘tuple‘>
<class ‘set‘>
<class ‘dict‘>
def func(a,b,c):
print(a,b,c)
print(type(func))
啊 = func
print(type(啊))
a = func
print(type(a))
<class ‘function‘>
<class ‘function‘>
<class ‘function‘>
import string
print(type(string))
<class ‘module‘>
class MyClass(object) :
pass
print(type(MyClass))
my_class = MyClass()
print(type(my_class))
<class ‘type‘>
<class ‘__main__.MyClass‘>
try:
x = 100
print(x)
except NameError:
print("NameError: ‘x‘ is not define")
import string
s = ‘abc‘
s[0] = ‘sd‘
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-ce449e8a2e6b> in <module>()
2
3 s = ‘abc‘
----> 4 s[0] = ‘sd‘
TypeError: ‘str‘ object does not support item assignment
import string
s = ‘ hjh 黄金进口件 你看见画江湖 mjjkj ‘
# 去除左右空格
print(s.strip())
# 去除左边空格
print(s.lstrip())
# 去除右边空格
print(s.rstrip())
# 输出原来的字符串
print(s)
hjh 黄金进口件 你看见画江湖 mjjkj
hjh 黄金进口件 你看见画江湖 mjjkj
hjh 黄金进口件 你看见画江湖 mjjkj
hjh 黄金进口件 你看见画江湖 mjjkj
s1 = ‘abc‘
s2 = ‘def‘
print(s1 + "\n" + s2)
abc
def
s = ‘abc def‘
print(s)
#把所有字符变成大写
print(s.upper())
# 把所有字符变大写后变回去
print(s.upper().lower())
# 首字母大写
print(s.capitalize())
abc def
ABC DEF
abc def
Abc def
s1 = ‘abcdefg‘
s2 = ‘lalalal‘
print(s1.index(‘f‘))
print(s1.index(‘fg‘))
print(s2.index(‘alal‘))
# 找不到时会报错需要异常处理
try:
print(s1.index(‘jjj‘))
except ValueError:
pass
5
5
1
s1 = ‘ddd‘
s2 = ‘sss‘
print(s1 == s2)
print(s1 < s2)
print(s1 > s2)
False
True
False
print(len(‘dsssss‘))
# print(len(None)) 会报错因为不支持
# 空字符串的长度是零
print(len(‘‘))
# 空格也是可以的
print(len(‘ ‘))
6
0
1
s = ‘‘
if s is None:
print(‘None‘)
p = None
if p is None:
print(‘p is None‘)
p is None
s = ‘abc,def,ghi‘
d = ‘abc..def..ghi‘
splitted = s.split(‘,‘)
print(type(splitted))
print(splitted)
# 可以按照任意分隔符切分
d1 = d.split(‘..‘)
print(type(d1))
print(d1)
# 可以按行分割
s1 = """ddd
dddgg
ggg
hhh
yy
yyy
jj
"""
# 方法一按照换行符分割
print(s1.split(‘\n‘))
# 方法二直接使用按行分割的方法
print(s1.splitlines())
#区别 按照换行符分割的特点是数量 只与换行符有关 而且使用方法不会捕捉到最后一个换行符
<class ‘list‘>
[‘abc‘, ‘def‘, ‘ghi‘]
<class ‘list‘>
[‘abc‘, ‘def‘, ‘ghi‘]
[‘ddd‘, ‘dddgg‘, ‘ggg‘, ‘hhh‘, ‘yy‘, ‘yyy‘, ‘jj‘, ‘‘]
[‘ddd‘, ‘dddgg‘, ‘ggg‘, ‘hhh‘, ‘yy‘, ‘yyy‘, ‘jj‘]
s = [‘ddd‘, ‘dddgg‘, ‘ggg‘, ‘hhh‘, ‘yy‘, ‘yyy‘, ‘jj‘]
# 使用空字符作为连接字符
print(‘‘.join(s))
print(‘\n‘)
# 使用换行符作为连接对象
print(‘\n‘.join(s))
print(‘\n‘)
# 使用短线作为连接对象
print(‘-‘.join(s))
ddddddggggghhhyyyyyjj
ddd
dddgg
ggg
hhh
yy
yyy
jj
ddd-dddgg-ggg-hhh-yy-yyy-jj
s = ‘assddf‘
# 判断是不是以字符a开头
print(s.startswith(‘a‘))
# 判断是不是以字符ass开头
print(s.startswith(‘ass‘))
# 判断是不是以字母s开头
print(s.startswith(‘s‘))
# 判断以...结尾
print(s.endswith(‘ddf‘))
# 判断是不是数字字母的组合
print(‘12333hjhjhdas‘.isalnum())
print(‘\t212133kjhjdashkjdas‘.isalnum())
# 判断是不是纯字母的形式
print(‘sjas‘.isalpha())
# 判断是不是数字
print(‘asada‘.isdigit())
# 判断是不是所有单词以大写字母开头
print(‘Hello World‘.istitle())
True
True
False
True
True
False
True
False
True
print(str(5))
print(str(5.))
print(str(55645.545))
print(str(-5454.5454))
print(int(‘5656‘))
print(float(‘5454.544‘))
print(float(‘555.566‘))
# 可以指定转换类型 默认是10进制
print(int(‘1010101010001‘, 2))
print(int(‘abcdef‘, 16))
print(int(‘77421‘, 8))
5
5.0
55645.545
-5454.5454
5656
5454.544
555.566
5457
11259375
32529
s = ‘asdf‘
l = list(s)
print(l)
[‘a‘, ‘s‘, ‘d‘, ‘f‘]
a = 100
b = 200
c = 300
if a == 100:
print(a)
elif b == 200:
print(b)
else:
print(c)
100
x = None
if x is None :
print(‘None‘)
else:
print(‘not None‘)
None
# range(开始数,结束数,间隔)
for i in range(0, 30, 5):
print(i)
# 不指定间隔时间隔是1
for i in range(5,10):
print (i)
0
5
10
15
20
25
5
6
7
8
9
s = 0
i = 1
while i <= 100:
s += i
i += 1
print(s)
5050
for i in range(0, 100):
if i < 10:
pass
elif i < 30:
continue
elif i < 35:
print(i)
else:
break
def func_name(arg1, arg2):
print(arg1, arg2)
return arg1, arg2
def func_name0(arg1, arg2):
print(arg1, arg2)
return arg1 + arg2
r = func_name(1, 2)
print(type(r))
# 可以说是是一个list
print(r[0],r[1])
print(func_name0(3, 5))
1 2
<class ‘tuple‘>
1 2
3 5
8
def func(x, y = 500):
return x + y
# 当调用时有全部参数时就会使用参数
print(func(100,800))
# 当有一个值未指定时,就会使用默认值
print(func(100))
900
600
def func(x, y = 500):
print(‘x = ‘, x)
print(‘y = ‘, y)
return x + y
print(func(100))
print(func(y = 300, x = 200))
print(func(x = 400))
x = 100
y = 500
600
x = 200
y = 300
500
x = 400
y = 500
900
# 表示从第一个参数开始所有参数都放到数组里去
def func(name, *numbers):
# 类型是只读数组原族
print(type(numbers))
print(numbers)
return ‘Done‘
func(‘Tom‘, 1, 2, 3, 4)
<class ‘tuple‘>
(1, 2, 3, 4)
‘Done‘
python基本知识 数据类型 字符串操作 if语句 is not 用法 for循环 while 循环
标签:star 数组 abc sign upper dig ber 分割 必须
原文地址:https://www.cnblogs.com/peng-free/p/9309184.html