python字符串位置交换_2018/5/15交换俩变量的值字符串的常见操作

''' 交换两个变量的值 '''

a = 4

b = 5

#交换方式一

c = 0

c = a

a = b

b = c

#交换方式二

a = a+b

b = a-b

a = a-b

#交换方式三

a,b = b,a

#交换方式四 异或方法

print("-"*80)

''' 字符串常见操作: find \ rfind index \ rindex \ count'''

my_str = "jack _ 123 and jack names world jack"

''' find 和 rfind index 和 rindex 查找'''

a = my_str.find("jack")

a1 = my_str.rfind("jack") #32

说明rfind就是从重复的right右边找,找到返回下标

print(a1)

print(a) #0 找到就返回其所在的索引第一个下标

b = my_str.find("lol")

print(b) #-1 找不到就返回-1

c = my_str.index("jack")

c1 = my_str.rindex("jack")

print(c) #0 找到就返回其所在的索引第一个下标,跟find方法一样

print(c1) #32 跟rfind方法一样

#d = my_str.index("lol")

#print(d) #找不到程序就崩

''' count 统计'''

d = my_str.count("jack")

print(d) #3 统计jack出现的次数

d1 = my_str.count("jacks")

print(d1) #0 找不到就返回0,注意它是把要找的字符串作为整体去找

''' replace 替换 默认替换掉所有相同的 返回一个替换后的字符串

格式: s.replace(old,new[,count]) --str

'''

print(my_str.replace("jack","杰克"))

print(my_str)

e = my_str.replace("jack","杰克")

'''

杰克 _ 123 and 杰克 names world 杰克

jack _ 123 and jack names world jack

杰克 _ 123 and 杰克 names world 杰克

'''

print(e) #杰克 _ 123 and 杰克 names world 杰克

把右边替换左边,返回替换后的字符串

#需要注意的是,替换并不是修改,我们知到字符串是不可变类型,如果要修改,新建变量来接收它

print(id(e)) #52025824 我们这里是新建的变量e接收替换的字符串,所以id变了

print(id(my_str)) #81047648

e1 = my_str.replace("jack","杰克"[0])

print(e1)

#杰 _ 123 and 杰 names world 杰 参数是0

#克 _ 123 and 克 names world 克 参数是1

e2 = my_str.replace("jack","杰克",4)

print(e2) #杰克 _ 123 and jack names world jack

,参数是几,就替换掉第几个jack

#不会的时候,如何查看帮助? q 退出

#help(my_str.replace)

''' 切割 split

注意:split() 传递的是参数,将来就按这个参数来切

如果什么都没传,就会按照不可见字符切(不可见字符:空格\n tab键\t)

'''

mystr = "hello every one come in my room"

d = mystr.split(" ")

print(d) #['hello', 'every', 'one', 'come', 'in', 'my',

'room']

d1 = mystr.split("l") #['he', '', 'o every one come in my

room']

# 双引号内是哪个字符就切割掉那个,并以那个切割字符为分界线,左右组成列表的元素

print(len(d1))

print(d1)

print(type(d)) #

''' capitalize 把字符串的第一个字符大写,不管这个字符串有多长 '''

e = mystr.capitalize()

print(e) #Hello every one come in my room

''' title 把字符串的每个单词的首字母大写 '''

e1 = mystr.title()

print(e1) #Hello Every One Come In My Room

''' upper 把所有字符串统统大写

lower 把所有字符串统统小写

'''

e3 = mystr.upper()

print(e3) #HELLO EVERY ONE COME IN MY ROOM

e4 = mystr.lower()

print(e4) #hello every one come in my room

''' startswith

检查字符串是否以 XX 开头,是则返回True,否则返回False

endswith

检查字符串是否以 XX 结尾,是则返回True,否则返回False

'''

name = "xi da da.txt"

f = name.startswith("xi")

print(f) #True

f1 = name.startswith("wang")

print(f1) #False

f2 = name.endswith(".txt")

print(f2) #True

f3 = name.endswith(".doc")

print(f3) #False

''' 左对齐ljust 、居中对齐center 、右对齐rjust '''

music_worlds = "我想带你去看浪漫的土耳其"

g = music_worlds.ljust(100)

g1 = music_worlds.center(100)

g2 = music_worlds.rjust(100)

print(g)

print(g1)

print(g2)

''' lstrip 删除左边空白字符

rstrip 删除右边空白字符

strip 删除两端空白字符

'''

new_str = " jack is our stars "

print(new_str)

h = new_str.lstrip()

print(h)

h1 = new_str.rstrip()

print(h1)

h2 = new_str.strip()

print(h2)

'''

jack is our stars

jack is our stars

jack is our stars

jack is our stars

'''

''' partition 把mystr以str分割成三部分,str前,str,和str后 '''

mystr = "hello world and hello every one hello"

i = mystr.partition("hello")

print(i) #('', 'hello', ' world and hello every one

hello')

print(type(i)) # 元组类型

''' rpartition 类似于partation()函数,不过是从右边开始 '''

i1 = mystr.rpartition("hello")

print(i1) #('hello world and hello every one ', 'hello',

'')

#试过并没有lpartation

''' splitlines 按行分割,返回一个包含各行作为元素的列表 \n '''

strs = "13312\njack sadwwada\n1452555\n"

j = strs.splitlines()

print(j) #['13312', 'jack sadwwada', '1452555']

''' isalpha 如果str 所有字符都是字母则返回True,否则返回False '''

str = "hello world 123"

k = str.isalpha()

print(k) #False

str1 = "hello world"

k1 = str1.isalpha()

print(k1) #False

str2 = "jack"

k2 = str2.isalpha()

print(k2) #True

''' isdigit 判断str 是否只包含数字,是返回True,否则返回False 跟上面对应

不再代码验证'''

''' isalnum 如果str所有字符都是字母或数字则返回True,否则返回False '''

''' isspace 如果str中只包含空格,则返回True,否则返回False '''

''' join str中每个字符后面插入str,构造出一个新的字符串 '''

names = ["aa","bb","1233"]

names2 = ("123",12,"ll")

names3 = {"as":123,"age":"18"}

a = "_"

print(a.join(names)) #aa_bb_1233 配合 split a = "" 什么也不写

print("*"*50)

#print(a.join(names2)) #上面都没打印

print(a.join(names3)) #as_age

print(type(a.join(names3))) #

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值