Python 字符串类型与运算

python字符串概述

python 单行字符串

单行字符串使用单引号或者双引号表示字符串内容。

temp = "请输入带有符号的温度值: " 
print("{}".format(temp))

temp = 'C'
print("{}".format(temp))

python 多行字符串

多行字符串使用三单引号或三双引号表示字符串内容。


'''
	没有赋值的三引号就是注释
'''
temp = '''请输入带有符号的温度值: 
				122℃
	   '''
print("{}".format(temp))

temp = """ A
		   B
		   C
	   """
print("{}".format(temp))

引入问题

为什么需要两类四种字符串表示方法?

问题答复

  1. 如何在字符串中包含单引号或者双引号?
temp = '汤姆的英文名称是:"Tom"'
print("{}".format(temp))

temp = "汤姆的英文名称的首字母是:'T'"
print("{}".format(temp))

  1. 如何在字符串中既包含单引号又包含双引号?
temp = '''汤姆的英文名称的首字母是:'T',英文全称是:"Tom" '''
print("{}".format(temp))

字符串取值

  1. python字符串支持 正向递增序号 和 反向递减序号;
  2. 使用[ ]获取字符串中一个或多个字符;
  3. 索引(获取单个字符):<字符串>[M];
  4. 基础切片(获取多个连续字符): <字符串>[M: N];(M开始,N结束,小于N)
  5. 进阶切片(获取多个非连续字符): <字符串>[M: N: K];

示例


'''
正向递增序号:0 1 2 3 4 5 6
示例的字符串:"ABCDEFG"
反向递减序号:-7 -6 -5 -4 -3 -2 -1
'''
strTmp = "ABCDEFG"
temp = strTmp [0]
print("正向递增首字符:{}".format(temp))
temp = strTmp [-1]
print("反向递减首字符:{}".format(temp))

temp = strTmp[0:2]
print("正向递增字符串片段:{}".format(temp))
temp = strTmp[-7:-5]
print("反向递减字符串片段:{}".format(temp))

''' <字符串>[M: N],M缺失表示至开头,N缺失表示至结尾 '''
temp = strTmp[0:]
print("正向递增全部字符串:{}".format(temp))
temp = strTmp[:-1] + strTmp [-1]
print("反向递减全部字符串:{}".format(temp))
temp = strTmp[:]
print("全部字符串:{}".format(temp))

temp = strTmp[::2]
print("正向递增字符串(每次递增2个间隙):{}".format(temp))
temp = strTmp[::-2]
print("反向递减字符串(每次递减2个间隙):{}".format(temp))
temp = strTmp[1:5:3]
print("正向递增字符串(每次递增3个间隙):{}".format(temp))

''' 打印引号可以使用转移符\ '''
print("汤姆的英文名称是: \"Tom\" ")
print('汤姆的英文名称是: \'Tom\' ')


'''
- 输入:1-12的整数,表示几月份
- 输出:输入整数对应的月份字符串
- 例如:输入3,输出 Mar(三月份)
'''

def main():
	''' 用于月份字符串输出  '''
	monthStr = "JanFebMarAprMayJunJulAugSeptOctNovDec"

	''' 接收输入 '''
	inputMonth = input("请输入月份序号(1-12):")

	''' 判断输入值是否合法 '''
	month = int(inputMonth)
	if month < 1 or month > 12:
		print("输入不合法!")
		return

	''' 输出输入月份对应的月份字符串 '''
	if month <= 8:
		start = 3 * (month - 1)
		end = 3 * month
	elif month == 9:
		start = 3 * 8 + 4 * (month - 8 - 1) 
		end = 3 * 8 + 4 * (month - 8)
	else:
		start = 3 * 8 + 4  + 3 * (month - 9 -1)
		end = 3 * 8 + 4 + 3 * (month - 9)
	monthTmp = monthStr[start:end]
		
	print("输入{}月份对应的输出月份字符串是:{}".format(inputMonth, monthTmp))

	
main()

python字符串操作符

在这里插入图片描述

python 字符串处理函数

在这里插入图片描述

python 字符串处理方法

  1. 字符串方法特指<str>.<b>()风格中的函数<b>()
  2. 常用的字符串方法:
    在这里插入图片描述

python 字符串类型的格式化

  1. 字符串格式化函数:str.format(),format()函数参数个数不限,位置不限,格式如下:
    在这里插入图片描述

  2. 示例:


strHello = "hello"
strWord = "word"

'''
	"{}".format(strHello)
	.之前表示字符串格式,{}称作槽
	.之后format函数用于接受输入参数(不仅仅是字符串参数)
	
'''
print("{}".format(strHello))
print("{}--{}".format(strHello, strWord))

''' 
	"{1}--{0}".format(strHello, strWord)
	槽中序号与format()函数参数位置对应,默认是一一对应,但是也可以修改。
	
'''
print("{1}--{0}".format(strHello, strWord))

# 指定输出宽度
print("{:10} {}".format(strHello, strWord))

# 指定输出宽度 + 字符不够用*补充 + 居中对齐
print("{0:*^10} {1}".format(strHello, strWord))

# 使用千位分隔符对数字输出
print("{0:,} {1}".format(1233.33, strWord))

# 使用千位分隔符对数字输出 + 精度
print("{0:,.3} {1}".format(1233.33, strWord))
print("{0:,.1} {1}".format(1233.33, strWord))

# 使用千位分隔符对数字输出 + 精度 + 指定类型
print("{0:,.3f} {1}".format(1233.33, strWord))
print("{0:,.3%} {1}".format(1233.33, strWord))

# 使用千位分隔符对数字输出 + 精度 + 指定类型 + 符号位
print("{0:+,.3f} {1}".format(-1233.33, strWord))
print("{0:-,.3f} {1}".format(1233.33, strWord))

python字符串统一编码格式

  1. Unicode编码,是统一的字符编码,覆盖几乎所有字符的编码方式;
  2. 编码范围:从0x0到0x10FFFF,每个编码对应一个字符;
  3. Python字符串中每个字符都是Unicode编码字符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值