
在英文的书写中,如果是开头的字母是需要区分大小写的,但是其中的更改大小写方法不适用于我们所学的python中。我们想要对字母进行大小写的更改,需要通过一些函数代码来对字母进行改变。没有接触过的小伙伴可要好好的学习下面的几段代码,在大写和小写的代码上,小编进行了区别,具体的代码示例如下:
首字母小写
如下方法将令给定字符串的第一个字符统一为小写。
1 2 3 4 5 6 | def decapitalize(string):
return str[:1].lower() + str[1:]
decapitalize( 'FooBar' ) # 'fooBar'
decapitalize( 'FooBar' ) # 'fooBar'
|
大写第一个字母
以下代码块会使用 title() 方法,从而大写字符串中每一个单词的首字母。
1 2 3 4 | s = "programming is awesome"
print(s.title())
# Programming Is Awesome
|
由大小写字母组成的字符串,请对它进行重新组合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | def ReverseArray(ch):
lens=len(ch)
begin=0
end=lens-1
while begin<end:
while ch[begin]>= 'a' and ch[end]<= 'z' and end>begin:
begin+=1
while ch[begin]>= 'A' and ch[end]<= 'Z' and end>begin:
end-=1
ch[begin],ch[end]=ch[end],ch[begin]
if __name__== '__main__' :
ch=list( 'AbcDef' )
ReverseArray(ch)
i=0
while i<len(ch):
print(ch[i])
i+=1
输出:
f
b
c
e
D
A
|
以上就是我们更改字母大小写的方法了,不知道小伙伴们学会了没有,理解的可以看大小写组合的字符串,也算是对我们本篇所学的内容进行一次活学活用。更多Python学习指路:PyThon学习网教学中心。