温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

32 字符串常用的方法 center find join s

发布时间:2020-07-31 08:23:44 来源:网络 阅读:267 作者:马吉辉 栏目:大数据

第八课 字符串中常用的方法:center方法 
# 字符串方法:center
# 作用是:将字符串在一定的宽度区域内居中显示
#  这个方法和我们之前将的format 中的 ^ 一样 

# ^
print("<" + "hello".center(30) + ">") # <            hello             >
# <      hello       >
print("<{:^30}>".format("hello"))     # <            hello             >

# 
print("<" + "hello".center(20, "*") + ">") # <*******hello********>
print("<{:*^20}>".format("hello"))         # <*******hello********>

-------------------------------------------------
第九课 字符串中常用的方法:find方法
在大字符串中来查找子子字符串 如果找到了,find方法就会返回子子字符串的第一个字符在大字符串中出现的位置 有就是索引 如果未找到,那么find方法就会返回-1
find方法有3个参数 第一个是要查找的子字符串 第二个参数是开始索引 第三个参数是结束索引  左闭右开 
# 字符串方法:find
s = "hello world"
print(s.find("world"))  # 6  w的位置索引为 6 

print(s.find("abc")) # -1   不存在返回-1 

print(s.find("o"))  # 4    返回第一个 0的索引 
print(s.find("o",6)) # 7   第二个0 出现在第7个位置 

print(s.find("l",5,9)) # -1
print(s.find("l",5,10)) # 9   左闭右开

-------------------------------------------------
第十课  字符串中常用的方法:join方法
# 字符串方法:join

# 用于连接序列中的元素,split方法的逆方法

list = ["a", "b", "c", "d", "e"]
s = '*'
print(s.join(list))  # a*b*c*d*e
print("xy".join(list)) # axybxycxydxye

# C:\abc\xyz
# /abc/xyz
# c:\usr\local\nginx\
# /usr/local/nginx/

# 这个join 在我们 linux 和windows 中很有帮助 不同的操作系统的路径用python代码如何表示: 要表示一个 linux环境的 路径   
# 先定义一个元组 

dirs = ('','usr','local','nginx','')
linuxPath = '/'.join(dirs)
print(linuxPath)                # /usr/local/nginx/

windowPath = ('C:' + '\\'.join(dirs))
print(windowPath)               # C:\usr\local\nginx\

#numList = [1,2,3,4,5,6]   # 这个跑出异常 必须要转化成字符串
#print("a".join(numList))

-------------------------------------------------------
第十一课  字符串中常用的方法:split方法 拆开之后为列表 

# 字符串方法:split方法  拆分成列表 
s1 = "a b c d e f"
print(s1.split())           # ['a', 'b', 'c', 'd', 'e', 'f']

s2= "a*b*c*d*e"
print(s2.split("*"))         # ['a', 'b', 'c', 'd', 'e']

path = "/usr/local/nginx"
pathList = path.split('/')
print(pathList)            # ['', 'usr', 'local', 'nginx']
windowPath = "D:" + "\\".join(pathList)
print(windowPath)           # D:\usr\local\nginx

--------------------------------------------------
第十二课 字符串中常用的方法: lower、upper和capwords函数
lower: 把所有的英文字母变成小写
upper:把所有的英文字母变成大写
capwords函数:会将独立的字符串中首字母大写

# 字符串方法:lower、upper和capwords函数
print("HEllo".lower())  # hello
print("hello".upper())  # HELLO

list = ["Python", "Ruby", "Java","KOTLIN"]
if "Kotlin" in list:
    print("找到Kotlin了")
else:
    print("未找到Kotlin")   # 未找到Kotlin 对大小写敏感

for lang in list:                    # 循环遍历一遍 这样把每一个单词都转化为小写
    if "kotlin" == lang.lower():
        print("找到Kotlin了")       # 找到Kotlin了
        break;

from string import capwords
s = "i not only like Python, but also like Kotlin"
print(capwords(s))
# I Not Only Like Python, But Also Like Kotlin

---------------------------------------------------
第十三课 字符串中常用的方法: replace方法和strip方法
replace方法:将一个字符串中出现的同样的子字符串 替换,这里最常用的就是把所有的控制台输入的空格 替换成空。例如:
"""
IDE = input('请输入IDE的名字')
findIDE = IDE.replace(' ', '').lower() # 把输入的空格转化为空 并且 把输入的值转化为小写 
"""

strip方法:截取前后空格

# 字符串方法:replace和strip

s = "abcdaedf"
print(s.replace("a", "12345")) # 12345bcd12345edf

print(s.replace("xyz","aa"))  # abcdaedf   不存在的话 就返回原字符串

print("   geekori.com   ".strip())  # geekori.com
print(" <  geekori.com  >  ".strip()) # <  geekori.com  >  只会截取前后的空格,不会截取 中间的空格 

langList = ["python", "java", "ruby", "scala", "perl"]
lang = "   python   "
if lang in langList:
    print("找到了python")
else:
    print("未找到python")   # 未找到python

if lang.strip() in langList:
    print("找到了python")
else:
    print("未找到python")  # 找到了python

s = "***  $$*  Hello * World   ***$$$  "
print(s.strip(" *$"))    # Hello * World             " *$"  这里面的三个是或的关系
-------------------------------------------------
第十四课 字符串中常用的方法:translate方法和maketrans方法

# 字符串方法:translate和maketrans

# translate:替换单个字符  maketrans方法:转化为字典 

s = "I not only like python, but also like kotlin."
table = s.maketrans("ak", "*$")  # 他的这个意思是把a替换为*      k替换为 $
print(table)             # {97: 42, 107: 36}  先转发为字典 ACLLZ吗值 
print(s.translate(table)) # I not only li$e python, but *lso li$e $otlin.
table1 = s.maketrans("ak", "*$", " ")  # 第三个参数 删除的意思   整体的意思是: 把a 转化为 * ,k 转化为 $  并删除掉中间的空格 
print(table1)                       # {97: 42, 107: 36, 32: None}
print(s.translate(table1))          # Inotonlyli$epython,but*lsoli$e$otlin.
# 把所有的 空格后删除了 
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI