기본 데이터 유형과 내장 방식 (숫자, 문자열)

A : INT 타입

1. 역할 : 연령의 설명, 신분증 번호

2. 정의 :

나이 = 10 # 연령 = INT (10)

3. 변환

3.1 순수한 디지털 INT의 문자열로 변환

res=int('100111')
print(res,type(res))
100111 <class 'int'>

3.2 (이해)

다른 이진 소수로 2.2.1 차례

진수 -> 진

print(bin(11)) # 0b1011

10 진수 -> 진수

print(oct(11)) # 0o13

10 진수 -> 육각

print(hex(11)) # 0xb

3.2.2 기타의 진수 시스템으로 전환

이진 -> 진수

print(int('0b1011',2)) # 11

(INT를 ( '0b1011', 2)) 인쇄 # 11

이진 -> 진수

print(int('0o13',8)) # 11

이진 -> 진수

print(int('0xb',16)) # 11

2 : 플로트 식

1 역할 : 설명하기위한 급여, 신장, 체중 등에

2, 정의

급여 = 3.1 # 급여 = 플로트 (3.1)

3 형식 변환

입술 = 플로트 ( "3.1")
인쇄 (입술, 유형 (고해상도))

사용 4

INT와 플로트가 내장되어하는 방법이 필요하지 않습니다
자신의 사용 + 상대적으로 수학 연산입니다

세 : 문자열 유형

1. 역할 : 특정 상태를 설명하는 데 사용 그런 사람의 이름, 외모 등

2, 정의

# msg='hello' # msg=str('msg')
# print(type(msg))

3 형식 변환

STR은 문자열로 변환되는 임의의 다른 유형이 될 수있다

# res=str({'a':1})
# print(res,type(res))
{'a': 1} <class 'str'>

4, 사용 : 내장 방법

4.1 우선 순위 파악

4.1.1 인덱스 값 (+ 순방향 역방향 권취 인출)에있어서 만 취할 수

MSG = '안녕하세요 세계'

앞으로 가져

msg='hello world'
print(msg[0])
print(msg[5])
h
 是一个空格

역 테이크

print(msg[-1])
d

단지 걸릴 수

msg[0]='H'     直接报错,字符串是不可变的
 File "C:/Users/Administrator/Desktop/3.10/03 字符串类型.py", line 22, in <module>
    msg[0]='H'
TypeError: 'str' object does not support item assignment

4.1.2 슬라이스 : 응용 프로그램 인덱스, 대형의 문자열에서 문자열의 복사본을 확장

MSG = '안녕하세요 세계'

마지막에 관계없이 케어

res=msg[0:5] #  从0开始4结束
print(res)
hello

단계

res=msg[0:5:2] # 0 2 4
print(res) # hlo

역 단계 (이해)

res=msg[5:0:-1]
print(res) #  olle
 olle

res=msg[:] # res=msg[0:11]  
print(res)
hello world

res=msg[::-1] # 把字符串倒过来
print(res)
dlrow olleh

4.1.3 길이 렌

msg='hello world'
print(len(msg))
11

작업의 4.1.4, 회원에의하지

대형의 문자열에서 하위 문자열이 있는지 여부를 결정

print("alex" in "alex is sb")
print("alex" not in "alex is sb")  # 不推荐使用
print(not "alex" in "alex is sb") 
True
False
False

4.1.5, 좌측 및 심볼 열의 우측 제거 스트립이며

기본 공간을 제거

msg='      egon      '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值
      egon      
egon

다른 문자는 제거

msg='****egon****'
print(msg.strip('*'))
egon

msg='****e*****gon****'
print(msg.strip('*'))
e*****gon  了解:strip只取两边,不去中间

신청

inp_user=input('your name>>: ').strip() # inp_user=" egon"
inp_pwd=input('your password>>: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
    print('登录成功')
else:
    print('账号密码错误')

4.1.6, 분할 분할 : 문자열 일부 구분에 따라 분할 할 수의 목록을 제공

기본 분리는 공간

info='egon 18 male'
res=info.split()
print(res)
['egon', '18', 'male']

구분 기호를 지정합니다

info='egon:18:male'
res=info.split(':')
print(res)

분리의 수 (이해)을 지정합니다

info='egon:18:male'
res=info.split(':',1)
print(res)

4.1.7, 자전거

info='egon:18:male'
for x in info:
    print(x)  依次输出

알 4.2 필요

4.2.1 스트립 lstrip, rstrip

msg='***egon****'
print(msg.strip('*'))
print(msg.lstrip('*'))
print(msg.rstrip('*'))
egon****
***egon
egon:18:male

4.2.2, 상하

msg='AbbbCCCC'
print(msg.lower())
print(msg.upper())
abbbcccc
ABBBCCCC

4.2.3, startswith, endswith

print("alex is sb".startswith("alex"))
print("alex is sb".endswith('sb'))
True
True

4.2.4, 형식

형식화 된 출력하기 전에 참조

4.2.5, 분할, rsplit : 잘라 문자열 목록

info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]

4.2.6 가입 : 문자열로 접합 목록을

l=['egon', '18', 'male']
res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res)
egon:18:male

4.2.7 대체

msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
print(msg.replace("you","YOU",1))  只替换第一个
YOU can YOU up no can no bb
YOU can you up no can no bb

4.2.8, isdigit에

순수한 숫자의 문자열 여부 결정

print('123'.isdigit())
print('12.3'.isdigit())
True
False

신청

# age=input('请输入你的年龄:').strip()
# if age.isdigit():
#     age=int(age) # int("abbab")
#     if age > 18:
#         print('猜大了')
#     elif age < 18:
#         print('猜小了')
#     else:
#         print('才最了')
# else:
#     print('必须输入数字,傻子')

4.3 알아보기

4.3.1, 발견, rfind, 인덱스, rindex, 수

msg='hello egon hahaha'
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))
print(msg.index('egon'))
1
6
1
6
# print(msg.find('xxx')) # 返回-1,代表找不到
# print(msg.index('xxx')) # 抛出异常

카운트 수

msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))
3

4.3.2, 센터, 빛, rjust, zfill

print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))
print('egon'.zfill(10))
***********************egon***********************
egon**********************************************
**********************************************egon
000000egon

4.3.3, expandtabs

msg='hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
hello world

4.3.4, captalize, swapcase, 제목

print("hello world egon".capitalize())   第一个单词首字母大写
print("Hello WorLd EGon".swapcase())   大小写互换
print("hello world egon".title())      每个单词首个字母大写
Hello world egon
hELLO wORlD egON
Hello World Egon

4.3.5 디지털 시리즈

4.3.6 다른입니다

print('abc'.islower())   判断是否小写
print('ABC'.isupper())   判断是否大写
print('Hello World'.istitle())   判断每个单词首个字母是否大写
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
print('ad'.isalpha()) # 字符串由由字母组成结果为True
print('     '.isspace()) # 字符串由空格组成结果为True
print('print'.isidentifier())      字符定义规范
print('age_of_egon'.isidentifier())
print('1age_of_egon'.isidentifier())
True
True
True
True
True
True
True
True
False

B'4 = NUM1 '에서 #Bytes
NUM2 = u'4'# 유니 코드, python3 유 유니 코드를 추가 할 필요 없다
num3 = '네'# 중국어 디지털
num4 = 'Ⅳ'# 로마 숫자를

# isdigit只能识别:num1、num2
# print(num1.isdigit()) # True
# print(num2.isdigit()) # True
# print(num3.isdigit()) # False
# print(num4.isdigit()) # False



# isnumberic可以识别:num2、num3、num4
# print(num2.isnumeric()) # True
# print(num3.isnumeric()) # True
# print(num4.isnumeric()) # True

# isdecimal只能识别:num2
# print(num2.isdecimal()) # True
# print(num3.isdecimal()) # False
# print(num4.isdecimal()) # False

추천

출처www.cnblogs.com/chenyoupan/p/12457769.html