python学习笔记:whitespace(空白符)及其判断、删除方法

一、string模块里的常量whitespace(string.whitespac)包含6个字符:

ASCII码 字符表示 显示 描述
9 \t HT,horizontal tab,水平制表符
10 \n 空一行 LF,line feed,换行
11      \x0b(十六进制) VT,vertical tab,垂直制表符
12 \x0c(十六进制) FF,form feed,换页
13 \r CR,carriage return,回车
32 \x20(十六进制) space,空格

二、判断是否为空白符:

       if S in string.whitespace: ......

       如果S是一个字符,则判断字符S是否为空白符;如果S为一个字符串,则判断字符串S是否全部为空白符。

三、删除空白符:

(1)删除换行——"\n":

  •    replace法:若字符串S包含换行符,可使用 S.replace("\n", "")。

(2)删除多余的空格——" ":(注意:" "不是"\t" )

  •   删增法:若字符串S包含多个连续的空格,可使用split+“ ”的方法:
    def done(S):
        newstring=""
        for every in S.split(" "):
            if every==" ":
                continue
            else:
                newstring+=every
                newstring+=" "
        return newstring
    

猜你喜欢

转载自blog.csdn.net/xiaozhimonica/article/details/84956487