Python:字串處理函數

來源:互聯網
上載者:User

標籤:center   name   int   rate   bin   span   let   count   python   

Split & Combine  #拆分和組合

#split() 通過指定分隔字元對字串進行切片lan = "python ruby c c++ swift"lan.split()[‘python‘, ‘ruby‘, ‘c‘, ‘c++‘, ‘swift‘]todos = "download python, install, download ide, learn"todos.split(‘, ‘)[‘download python‘, ‘install‘, ‘download ide‘, ‘learn‘]‘,‘.join([‘download python‘, ‘install‘, ‘download ide‘, ‘learn‘])‘download python,install,download ide,learn‘

 

Substitue #替換

#replace()s = ‘I like C. I like C++. I like Python‘s.replace(‘like‘, ‘hate‘)‘I hate C. I hate C++. I hate Python‘s.replace(‘like‘, ‘hate‘, 1)‘I hate C. I like C++. I like Python‘

Layout #布局
align = ‘Learn how to align‘align.center(30)‘      Learn how to align      ‘align.ljust(30)‘Learn how to align            ‘align.rjust(30)‘            Learn how to align‘ralign = align.rjust(30)ralign.strip()‘Learn how to align‘

 

Other useful tools

py_desc = "Python description: Python is a programming language that lets you work quickly and integrate systems more effectively."py_desc.startswith(‘Python‘)Truepy_desc.endswith(‘effectively.‘)Truepy_desc.find(‘language‘)44py_desc.isalnum()Falsepy_desc.count("Python")2py_desc.strip(‘.‘)‘Python description: Python is a programming language that lets you work quickly and integrate systems more effectively‘py_desc.upper()‘PYTHON DESCRIPTION: PYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.‘py_desc.title()‘Python Description: Python Is A Programming Language That Lets You Work Quickly And Integrate Systems More Effectively.‘

 

New style in Python 3.6

print(‘%s %s‘ % (‘one‘, ‘two‘))print(‘{} {}‘.format(‘one‘, ‘two‘))print(‘%d %d‘ % (1, 2))print(‘{} {}‘.format(1, 2))one twoone two1 21 2print(‘{1} {0}‘.format(‘one‘, ‘two‘))two onea = 5b = 10print(f‘Five plus ten is {a + b} and not {2 * (a + b)}.‘)Five plus ten is 15 and not 30.name = "Joshua"question = "hello"print(f"Hello, {name}! How‘s it {question}?")Hello, Joshua! How‘s it hello?

 

Python:字串處理函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.