基于python下的元组,字符串

元组的定义:

Tuple(元组)与列表相似,不同之处在于元组的元素不能修改.
元组表示多个元素组成的序列.
元组在python开发中,有特定的应用场景,用于存储一串信息,数据之间使用,分隔.
元组用()定义
# 列表中通常保存相同类型的数据,而元组中通常保存不同类型的数据

初始元组:

In [1]: xfl_tuple = ('xuefeilong',21,175)  定义一个简单的元组

In [2]: type(xfl_tuple)   查看类型为元组类型
Out[2]: tuple

In [3]: xfl_tuple[0]   从零索引取值
Out[3]: 'xuefeilong'

In [4]: xfl_tuple[1]
Out[4]: 21

In [5]: xfl_tuple[2]
Out[5]: 175

In [6]: empty_tuple = ()   定义一个空元组

In [7]: type(empty_tuple)  查看类型为元组类型
Out[7]: tuple

In [8]: empty_tuple.   查看元组的功能
empty_tuple.count  empty_tuple.index  

In [8]: single_tuple=()

In [9]: single_tuple=(1)  定义一个数据的元组必须加,号

In [10]: type(single_tuple)  查看类型为整形,这是初学者容易犯错
Out[10]: int

In [11]: single_tuple=(1,)   定义一个只有一个元素的元组,
必须加,号声明元组类型,加,表示其是一个元组,不然是整型,说明,是元组的标志

In [12]: type(single_tuple)
Out[12]: tuple

这里写图片描述
取值和索引以及统计元素个数:

# 元组列表均是从零索引
xfl_tuple = ('薛飞龙', 21, 175, '薛飞龙')
# 1.取值和取索引
print xfl_tuple[0]
print xfl_tuple.index('薛飞龙')
# 取索引的时候如果有重复的取第一个索引值
# 统计字数有几个薛飞龙
print xfl_tuple.count('薛飞龙')
# 统计元组中包含元素的个数
print len(xfl_tuple)

这里写图片描述
元组的输出:

# 最简单的if循环打印元组
xfl_tuple = ('薛飞龙', 21, 175)
# python规范在,号后面得加空格
for my_info in xfl_tuple:
    print my_info
# 格式化字符串后面(),本质上都是一个元组
xfl_tuple = ('薛飞龙', 21, 175)
print '%s的年龄是%d,身高是%d' %xfl_tuple

这里写图片描述
元组列表的类型转换:

In [13]: num_list = [1,2,3,4]   定义一个列表

In [14]: type(num_list)  查看类型为列表
Out[14]: list

In [15]: num = tuple(num_list)  转换成元组类型

In [16]: type(num)   查看已经转换为元组类型
Out[16]: tuple

In [17]: num1 = list(num)  再次转换成列表

In [18]: type(num1)  查看已经转换为列表类型
Out[18]: list

这里写图片描述
python中单双引号用法的区别:

In [19]: print 'hello'   单双引号均可以正常输出
hello

In [20]: print "hello"
hello

In [21]: print 'he's boy'    单引号输出报错
  File "<ipython-input-21-c3d94099ffdb>", line 1
    print 'he's boy'
              ^
SyntaxError: invalid syntax


In [22]: print "he's boy"    用""进行输出
he's boy

In [23]: print 'he\'s boy'   加入\进行转义
he's boy

这里写图片描述
什么是字符串:

字符串或串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。
在程序设计中,字符串(string)为符号或数值的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。

字符串的输出:

str1 = 'hi'
for char in str1:
    print char
str2 = u'我的宠物叫粉条'
# 直接写入中文回报错,得加u声明这是uniq编码格式
for char in str2:
    print char

这里写图片描述
统计字符串长度以及索引:

hello_str = 'hello python hello'
# 1.统计字符串的长度空格也算
print len(hello_str)
# 2.统计某一个小字符串(子字符串)出现的次数
# 单个字符也可以统计
# 不存在的字符串/字符会输出0
print hello_str.count('llo')
print hello_str.count('o')
print hello_str.count('ww')
# 3.某一个子字符串/字符出现的位置
# 输出的索引均为第一个字符/字符串出现的位置
# 如果字符串不存在,则会报错
print hello_str.index('llo')
print hello_str.index('o')
# print hello_str.index('ww')

这里写图片描述
字符串的常用功能01:

# 1.判断字符串是否只包含数字
num_str = '1'
print num_str
print num_str.isdigit()
# 2.判断字符串是否以指定的字符/子字符串开头
# 如果字符串不存在不返回False
hello_str = 'hello python'
print hello_str.startswith('h')
print hello_str.startswith('hello')
print hello_str.startswith('ww')
# 3.判断字符串是否以指定的字符串/字符结束
# 如果字符串不存在不返回False
print hello_str.endswith('n')
print hello_str.endswith('python')
print hello_str.endswith('ww')

这里写图片描述
字符串的常用功能02:

hello_str = 'hello python'
# 4.查找指定的字符串,返回的是一个索引
# 如果查找的指定字符串不存在,程序不会报错,会返回-1
print hello_str.find('p')
print hello_str.find('llo')
print hello_str.find('ww')
# 5.替换字符串
print hello_str
print hello_str.replace('python','world')
print hello_str
# 6.判断字符串是否有空格
null = ''
print null.isspace()
null3 = '\t'
print null3.isspace()  # /t置表符也被看作空格
null2 = '\n'
print null2.isspace()  # /n换行符也被看作空格

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值