標籤: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:字串處理函數