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‘) True py_desc.endswith(‘effectively.‘) True py_desc.find(‘language‘) 44 py_desc.isalnum() False py_desc.count("Python") 2 py_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 two one two 1 2 1 2 print(‘{1} {0}‘.format(‘one‘, ‘two‘)) two one a = 5 b = 10 print(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?
原文地址:https://www.cnblogs.com/kumata/p/9052072.html
时间: 2024-11-02 13:48:53