美文网首页
Python编程快速上手-第六章

Python编程快速上手-第六章

作者: parkwu | 来源:发表于2018-07-02 13:07 被阅读0次

    处理字符串

    原始字符串
    在print内部加上 r‘ 后面的转义字符都被忽略

    >>> print(r'That is Carol\'s cat.')
    That is Carol\'s cat.
    

    三重引号的多行字符串

    print('''Dear Alice,
    Eve's cat has been arrested for catnapping, cat burglary, and extortion.
    Sincerely,
    Bob''')
    

    多行注释

    在函数之前写入一些介绍函数的信息

    """This is a test Python program.
    Written by Al Sweigart al@inventwithpython.com
    This program was designed for Python 3, not Python 2.
    """
    def spam():
    """This is a multiline comment to help
    explain what the spam() function does."""
    print('Hello!')
    

    字符串的下标、切片、in 、not in

    spam[3]
    spam[0:5]

    字符串的方法upper()、lower()、isupper()和islower()

    upper()和lower()是临时改变字符串
    isupper()和islower()返回布尔值

    isX 字符串方法

    用来判断输入的有效性很好

    isalpha()返回True,如果字符串只包含字母,并且非空;
    isalnum()返回True,如果字符串只包含字母和数字,并且非空;
    isdecimal()返回True,如果字符串只包含数字字符,并且非空;
    isspace()返回True,如果字符串只包含空格、制表符和换行,并且非空;
    istitle()返回True,如果字符串仅包含以大写字母开头、后面都是小写字母的单词。

    字符串的startswith()和endswith()方法

    检查字符串的开始和结尾,返回True、False

    字符串方法join()和split()

    join()传入一个字符串列表,join前面的字符串作为穿如字符串的分隔符。

    >>> ', '.join(['cats', 'rats', 'bats'])
    'cats, rats, bats'
    >>> ' '.join(['My', 'name', 'is', 'Simon'])
    'My name is Simon'
    >>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
    'MyABCnameABCisABCSimon'
    

    split()针对一个字符串调用,返回一个字符串列表,可指定分割方法

    >>> 'My name is Simon'.split()
    ['My', 'name', 'is', 'Simon']
    >>> 'MyABCnameABCisABCSimon'.split('ABC')
    ['My', 'name', 'is', 'Simon']
    >>> 'My name is Simon'.split('m')
    ['My na', 'e is Si', 'on']
    >>> spam.split('\n')
    

    文本对其用rjust()、ljust()和center()

    ljust()、rjust(),文本左对齐、右对齐
    文本长度

    >>> 'Hello'.rjust(10)
    '                Hello'
    

    填充符号

    >>> 'Hello'.rjust(20, '*')
    '***************Hello'
    

    center()
    文本居中

    >>> 'Hello'.center(20)
    '            Hello '
    >>> 'Hello'.center(20, '=')
    '=======Hello========'
    

    用strip()、rstrip()和lstrip()删除空白字符

    strip()、rstrip()和lstrip()无参数时删除收尾的空白字符
    spam.strip('ampS'),存储的字符串两端,删除出现的a、m、p 和大写的S。传入strip()方法的字符串中,字符的顺序并不重要:strip('ampS')做的事情和strip('mapS')或strip('Spam')一样。

    >>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
    >>> spam.strip('ampS')
    'BaconSpamEggs'
    

    用pyperclip 模块拷贝粘贴字符串

    向计算机剪贴板发送文本,或从剪贴板接收文本。

    >>> import pyperclip
    >>> pyperclip.copy('Hello world!')
    >>> pyperclip.paste()
    'Hello world!'
    

    6.3项目:口令保管箱
    看不懂,暂时放弃

    6.4项目:在Wiki 标记中添加无序列表
    6.7 实践项目

    tableData = [['apples', 'oranges', 'cherries', 'banana'],
                ['Alice', 'Bob', 'Carol', 'David'],
                ['dogs', 'cats', 'moose', 'goose']]
    你的printTable()函数将打印出:
      apples   Alice   dogs
     oranges     Bob   cats
    cherries   Carol  moose
      banana   David  goose
    

    思路:
    尝试用for循环组合成新的数组,失败
    1.python不能静态生成2维数组
    2.嵌套2个for循环,new_table.append(sun_list)得不到想要的结果
    3.可以尝试一下用1维数组分割成2维数组。

    使用for去循环下标在这是一个不错的选择

    def print_table(table_dict):
        char_len = []
        char_list = []
        new_table = []
        
        for col in range(len(table_dict[0])):
            for row in range(len(table_dict)):
                char_len.append(len(table_dict[row][col]))
                char_list.append(table_dict[row][col])
        # for chars in table_dict:
        #     for char in chars:
        #         char_list.append(char)
        #         char_len.append(len(char))
    
        print(char_list)
        print(char_len)
        max_char_length = max(char_len)
        print(max_char_length)
        for col in range(len(table_dict[0])):
            for row in range(len(table_dict)):
                char = table_dict[row][col].rjust(max_char_length+1)
                print(char,end='')
            print('')
    

    相关文章

      网友评论

          本文标题:Python编程快速上手-第六章

          本文链接:https://www.haomeiwen.com/subject/nhchuftx.html