ITeye博客

从零开始学Python

onlynima · 2015-08-03 11:31 · Python · 编程语言 · Markdown

第一章:字符串操作

1.格式化字符串

(1)%字符,说明转换的开始

(2)转换标志:-表示左对齐,+表示要在转换值之前加上正负号;””(空白字符)表示正数之前保留空格;0表示转换之后位数不足用0补齐;

(3)最小字段宽度:可以指定转换字符之后的宽度,若为*,则宽度从元组中读出

(4)点(.)+精度:可以设置精度值

 

2.简单转换

‘ hello %%d’%world

输出:hello world

 

3.字符串方法

   find方法:在较长的字符串中查找字符串,返回最左端的索引,若没有,则返回-1

     ‘hello world hello’.find(‘hello’)

      输出:0

   特别地:find(‘查找字符串’,起始点,结束点)结束点可以没有,

   join方法:在队列中添加元素(队列元素只能是字符串)

   lower方法返回字符串的小写字母

     name=’ZQ’

     names=[‘zq’,’zq2’]

     if name.lower() in names:print(‘OK’)

输出:OK

   replace方法返回某字符串的所有匹配项均被替换之后的字符串

     ‘hello zq,zq your are strong’.replace(‘zq’,’world’)

     输出:hello world,world your are strong

   split方法是join的逆方法,将字符串分割为序列

     ‘1+2+3+4’.split(‘+’)

     输出:[1,2,3,4]

   strip方法返回出去左右两侧(不包括内部)空格的字符串:

     ‘     hello    ’.strip()

     输出:‘hello’

   translate方法处理单个字符,将其替换为其他字符

     //‘hello zq’.translate(,)

 

<!--EndFragment-->

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。