python之基本语法【字符 串方法】

字符串的下标,从0开始的(从前往后),从-1开始(从后往前数)

1.切片

str = "pyton hello"
print("str[2]=", str[2])
print("str[-2]=", str[-2])
# 字符串的切片操作,【起始位置:终止位置】:去头不取尾,虫开始位置开始,到终止位置的前一个
str2 = str[0:10]
print("str2=", str2)
# [起始位置:终止位置:步长”]:步长为多少,就是多少个去第一个
st2="1234567890"
print("str2[::2]==",st2[::2])

打印结果
str[2]= t str[-2]= l str2= pyton hell str2[::2]== 13579

 2.字符串拼接

1)第一种是使用:+

2)第二种是使用jion

字符串X:jion(字符串1,字符串2,字符串3.......):字符串1 字符串x字符串2字符串x字符串3字符串x.....

str = "pyton hello"
str2="1234567890"
print("str.join(st2)===",str.join(str2))
print("','.join(str1,str2)===",','.join((str,str2)))
str3="asdfghjkl"
print("str3.join((str,str2))===",str3.join((str,str2)))
str4="zxcvbnm"
print("str3.join((str,str2,str4))===",str3.join((str,str2,str4)))

打印结果:
str.join(st2)=== 1pyton hello2pyton hello3pyton hello4pyton hello5pyton hello6pyton hello7pyton hello8pyton hello9pyton hello0 ','.join(str1,str2)=== pyton hello,1234567890 str3.join((str,str2))=== pyton helloasdfghjkl1234567890 str3.join((str,str2,str4))=== pyton helloasdfghjkl1234567890asdfghjklzxcvbnm

3.字符串转移

1)\n:换行符

2)\t:制表符,不足4位自动补齐四位

print("7777\n8888\n99999")
print("7777\t8888\t99999")



打印结果
7777
8888
99999
7777	8888	99999

 注意:不想转义,在字符串前加小写r

4.字符串的方法

方法的调用格式:字符串.方法名()

1)find:查找字符串片在字符串中的下标位置(起始位置)

不存在字符串片段返回-1

str='qwertwyuiop'
print(str.find("ert"))
print(str.find("erm"))
print(str.find("w")) #从前往后找,返回的是一个被找到字符串片段的起始位置
print(str.find("w",2))
print(str.find("w",3))


打印结果
2
-1
1
5
5

2)count:统计字符串片段在字符串中出现的次数

 注意:字符串片段不存在返回0

str='qwertwyuiop'

print(str.count("w")) 
print(str.count("a"))

打印结果
2
0

3)replace:替换指定字符串片段

参数1:要替换的字符串片段

参数2:被替换之后的字符串片段

参数3:指定替换的次数(默认替换所有的)

str='qwertwyuiop'

print(str.replace("w","W"))
print(str.replace("w","W",1))

打印结果
qWertWyuiop
qWertwyuiop

# [起始位置:终止位置:步长”]:步长为多少,就是多少个去第一个

st2="1234567890"
print("str2[::2]==",st2[::2])

4)split分隔

 

1.指定分隔点对字符串进行分隔
参数1:分隔点
参数2:分隔的次数(默认找到所有的分隔点进行分隔) 
str = 'qwertwyuiop'
print(str.split("w", 2))  # ['q', 'ert', 'yuiop']
print(str.split("w", 1))  # ['q', 'ertwyuiop']
print(str.split("w"))  # ['q', 'ert', 'yuiop']

  

5)upper将小写字母转为大写字母,lower将大写字母转为小写字母

str = 'qwerTwyuiop'
print(str.upper())  # QWERTWYUIOP
print(str.lower())  # qwertwyuiop

 

6)format使用

相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’

使用方法由两种:b.format(a)和format(a,b)。

1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型

2.单个参数可以多次输出,参数顺序可以不相同

3.填充方式十分灵活,对齐方式十分强大

4.官方推荐用的方式,%方式将会在后面的版本被淘汰

 

1、基本用法

  (1)不带编号,即“{}”,按照默认顺序替换变量

  (2)带数字编号,可调换顺序,即“{1}”、“{2}” ,按照数字顺序替换变量

  (3)带关键字,即“{a}”、“{tom}”

注意:

foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……

也可以不输入数字,这样也会按顺序来填充

同一个参数可以填充多次,这个是format比%先进的地方

print('我们欢迎{}来到中国的 {}'.format('小米','beijing'))  # 不带字段
print('我们欢迎{0}来到中国的 {1}'.format('小米','beijing'))   # 带数字编号
print('我们欢迎{0}来到中国的 {1},参观{0}'.format('小米','beijing'))  # 打乱顺序
print('我们欢迎{1}来到中国的 {1},参观{0}'.format('小米','beijing'))  # 打乱顺序

#(2)通过Key来填充 print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字【world hello world
打印结果:
我们欢迎小米来到中国的 beijing
我们欢迎小米来到中国的 beijing
我们欢迎小米来到中国的 beijing,参观小米
我们欢迎beijing来到中国的 beijing,参观小米

  (4)通过下标来填充

names = ['beijing', 'xiaoming']
print('hello {names[0]} i am {names[1]}'.format(names=names))
print('hello {0[0]} i am {0[1]}'.format(names))

打印结果:
hello beijing i am xiaoming
hello beijing i am xiaoming

  (5)通过字典的Key

names = {'name': 'beijing', 'name2': 'shanghai'}
print('hello {names[name]} i am {names[name2]}'.format(names=names))  # hello beijing i am shanghai

  (6)通过对象的属性

class Names:
    name1 = 'beijing'
    name2 = 'shanghai'


print('hello {names.name1} i am {names.name2}'.format(names=Names))  # hello beijing i am shanghai

  (7)format作为函数

f = 'hello {0} i am {1}'.format
print(f('Kevin' ,'Tom'))
 
#输出
hello Kevin i am Tom

  (8)填充与对齐

填充常跟对齐一起使用
^、<、>分别是居中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充


print('{:>8}'.format('6369'))
print('{:0>8}'.format('6369'))
print('{:F>8}'.format('6369'))


打印结果:
    6369
00006369
FFFF6369

  

原文地址:https://www.cnblogs.com/mumianhuasayyes/p/15865665.html