파이썬 인터뷰 백 개 질문 - 핵심 기초 (2)

디렉토리

  1. 어떻게 문자 문자열로 변환 할 수 있는지 여부를 감지하는
  2. 어떻게 문자열을 반대하는
  3. 정수 및 부동 소수점 형식
  4. 이스케이프 문자 문자열 형식뿐만 아니라
    사용 10.print 기능

문자 문자열로 변환 할 수 있는지 여부를 감지하는 방법 06

그림 삽입 설명 여기

s1 = '12345'
print(s1.isdigit())
s2 = '12345e'
print(s2.isalnum())

개요
그림 삽입 설명 여기

문자열을 반대하는 방법 07

그림 삽입 설명 여기

s1 = 'abcde'
s2 = ''
for c in s1:
	s2 = c + s2
print(s2)
edcba

s2 = s1[::-1]

개요
그림 삽입 설명 여기

08. 정수 및 부동 소수점 형식

그림 삽입 설명 여기

#格式化整数
n = 1234
print(format(n, '10d'))
      1234
print(format(n, '0>10d'))
0000001234
print(format(n, '0<10d'))
1234000000

#格式化浮点数
x1 = 123.456
x2 = 30.1
print(format(x1, '0.2f'))	#保留小数点后两位
123.46
print(format(x2, '0.2f'))
30.10

#描述format函数
print(format(x2, '*>15.4f'))	#右对齐,********30.1000
print(format(x2, '*<15.4f'))	#左对齐,30.1000********
print(format(x2, '*^15.4f'))	#中间对齐,****30.1000****
print(format(123456789, ','))	#用千位号分隔,123,456,789
print(format(1234567.1235648, ',.2f'))	# 1,234,567.12
#科学计数法输出(e大小写都可以)
print(format(x1, 'e'))	# 1.234560e+02
print(format(x1, '0.2e'))	# 1.23e+02

개요
그림 삽입 설명 여기

09. 문자 및 문자열 이스케이프 형식

그림 삽입 설명 여기

#同时输出单引号和双引号
print('hello "world"')	# hello "world"
print("hello 'world'")	# hello 'world'
print('"hello" \'world\'')	# "hello" 'world'

#让转义符失效(3种方法:r、repr和\)
print(r'Let \'s go!')	# Let \'s go!
print(repr('hello\nworld'))	# 'hello\nworld'
print('hello\\nworld')	# hello\nworld

#保持原始格式输出字符串
print('''
		hello
			 world
			 ''')	#三个双引号也可以

개요
그림 삽입 설명 여기

10.print 사용 기능

디폴트 인쇄 공백으로 분리 기능 및 개행

#print函数默认空格分隔,并且换行
#用逗号分隔输出的字符串
print('aa', 'bb', sep=',')	# aa,bb
print('aa', 'bb', sep='中国')	# aa中国bb

#不换行
print('hello', end=' ')
print('world')	# hello world

#格式化
s = 's'
print('This is %d word %s' % (10, s))
# 占位符有点过时了,推荐使用format

개요
그림 삽입 설명 여기

게시 11 개 원래 기사 · 원의 찬양 3 · 조회수 927

추천

출처blog.csdn.net/qq_36551226/article/details/104173645