Python之一些容易被忽略的小细节(一)

        字符串操作

        一、大小写转换

        1. capitalize():第一个字母大写。

s = 'aBcDeFg'
s1 = s.capitalize()
print(s1)

        运行结果:

Abcdefg

Process finished with exit code 0

        2. lower():全部换为小写。

s = 'aBcDeFg'
s1 = s.lower()
print(s1)

        运行结果:

abcdefg

Process finished with exit code 0

        3. upper():全部转化为大写。

s = 'aBcDeFg'
s1 = s.upper()
print(s1)
        运行结果:

ABCDEFG

Process finished with exit code 0

        4. swapcase():大小写相互转化。

s = 'aBcDeFg'
# 第一次转
s1 = s.swapcase()
print(s1)
# 第二次转
s2 = s1.swapcase()
print(s2)

        运行结果:

AbCdEfG
aBcDeFg

Process finished with exit code 0

        5. casefold():除了英文外,将其他字符小写,效率略低,建议少用。

s2 = "БBß"  # 俄美德
print(s2)
print(s2.lower())
print(s2.casefold())
        运行结果:

БBß
бbß
бbss

Process finished with exit code 0

        6. title():每个单词首字母大写,被特殊字符隔开也算。

s3 = "hello eggon,java*nodejs_麻花藤dashabi"
ret = s3.title()
print(ret)

        运行结果:

Hello Eggon,Java*Nodejs_麻花藤Dashabi

Process finished with exit code 0

        二、字符串切割

        1. center(居中长度,“空白覆盖”):居中。

s5 = "周杰伦"
ret = s5.center(10, "*")  # 10, 把原字符串放中间.其余位置补*
print(ret)

        运行结果:

***周杰伦****

Process finished with exit code 0

        说明:若是居中长度小于字符串长度,原样打印字符串。若是刚好不是正中间,优先往左靠一个字符(尽量居中)。

        2. 去空格strip(),lstrip(),rstrip():去左右两边空白,去左空白,去右空白。

s7 = " python nodejs java "
ret = s7.strip()  # 去掉左右两端的空格
print(ret)
ret = s7.lstrip()  # 去掉左边空格
print(ret)
ret = s7.rstrip()  # 去掉右边空格
print(ret)

        运行结果:

python nodejs java
python nodejs java 
 python nodejs java

Process finished with exit code 0

        strip()也可以去掉指定的元素。

s7 = "abcdefgabc"
print(s7.strip("abc"))
print(s7.strip("ef"))  # 无法去除中间元素

        运行结果:

defg
abcdefgabc

Process finished with exit code 0

        3. replace():字符串替换。

s8 = "Hello,Hold your dream"
ret = s8.replace('your', 'our') 
print(ret)

        运行结果:

Hello,Hold our dream

Process finished with exit code 0

        4. split():字符串切割。

s = "Hello,Hold your dream"
ret = s.split("o")
print(ret)

        运行结果:

['Hell', ',H', 'ld y', 'ur dream']

Process finished with exit code 0

        说明:切割之后返回列表。

        三、字符串查找

        1. startswith(),endswith():判断是否以某字符(串)开头或者结尾,返回值为布尔类型。

s = "Hello,Hold your dream"
print(s.startswith("H"))
print(s.endswith("M"))

        运行结果:

True
False

Process finished with exit code 0

        2. count():查找字符(串)出现的次数

s = "Hello,Hold your dream"
re = s.count("o")
print(re)

        运行结果:

3

Process finished with exit code 0

        3. find():查找字符(串)的位置。

s = "Hello,Hold your dream"
re = s.find("Ho")
print(re)

        运行结果:

6

Process finished with exit code 0

        注意:返回的是字符串的第一个值的角标。

        4. index():找到字符串索引位置。        

s = "Hello,Hold your dream"
re = s.index("Ho")
# re1 = s.index("Hl") # ValueError: substring not found
print(re)

        运行结果:

6

Process finished with exit code 0

        和index()不同的是find()在找不到substring时不会抛出异常,而是会返回-1,因此不会影响程序的执行。

s = "Hello,Hold your dream"
re = s.index("Ho")
re1 = s.find("Hl")  # 不会抛出错误,返回-1
print(re)
print(re1)

        运行结果:

6
-1

Process finished with exit code 0

        三、列表操作

        1. 切片

lst = ["麻花藤", "王剑林", "马芸", "周鸿医", "向华强"]
print(lst[0:3])  # ['麻花藤', '王剑林', '马芸']
print(lst[:3])  # ['麻花藤', '王剑林', '马芸']
print(lst[1::2])  # ['王剑林', '周鸿医'] 也有步长
print(lst[2::-1])  # ['马芸', '王剑林', '麻花藤'] 也可以倒着取
print(lst[-1:-3:-2])  # 倒着带步长

        运行结果:

['麻花藤', '王剑林', '马芸']
['麻花藤', '王剑林', '马芸']
['王剑林', '周鸿医']
['马芸', '王剑林', '麻花藤']
['向华强']

Process finished with exit code 0

        2. 删除pop(),remove(),clear(),del()的练习与区别。

lst = ["麻花藤", "王剑林", "李嘉诚", "王富贵"]
print(lst)
deleted = lst.pop()  # 删除最后一个
print("被删除的", deleted)
print(lst)
el = lst.pop(2)  # 删除2号元素
print(el)
print(lst)
lst.remove("麻花藤")  # 删除指定元素
print(lst)
# lst.remove("哈哈") # 删除不存在的元素会报错
# # print(lst)
lst.clear()  # 清空list
print(lst)
# 删除,删除角标为12的元素
del lst[1:3]
print(lst)

        运行结果:

['麻花藤', '王剑林', '李嘉诚']
李嘉诚
['麻花藤', '王剑林']
['王剑林']
[]
[]

Process finished with exit code 0

        3. 修改

lst = ["Python", "Java", "Golang", "Perl", "JavaScript", "Node.js", "C#", "D"]

# 修改
lst[3] = "C++"
print(lst)
# 切片修改,步长为3,注意取值的个数
lst[1:4:3] = ["VB"]
print(lst)
# 切片修改,步长为默认值1
lst[1:4] = ["I like Python"]
print(lst)

          运行结果:

['Python', 'Java', 'Golang', 'C++', 'JavaScript', 'Node.js', 'C#', 'D']
['Python', 'VB', 'Golang', 'C++', 'JavaScript', 'Node.js', 'C#', 'D']
['Python', 'I like Python', 'JavaScript', 'Node.js', 'C#', 'D']

Process finished with exit code 0

        4. 迭代查询

lst = ["Python", "Java", "Golang", "Perl", "JavaScript", "Node.js", "C#", "D"]
for el in lst:
    print(el)

        运行结果:

Python
Java
Golang
Perl
JavaScript
Node.js
C#
D

Process finished with exit code 0

        5. 其它操作

        reverse():列表翻转

lst = ["Python", "Java", "Golang", "Perl", "JavaScript", "Node.js", "C#", "D"]
lst.reverse()
print(lst)
        运行结果:

['D', 'C#', 'Node.js', 'JavaScript', 'Perl', 'Golang', 'Java', 'Python']

Process finished with exit code 0        



你可能感兴趣的:(Python)