码迷,mamicode.com
首页 > 编程语言 > 详细

Python:字符串处理函数

时间:2018-05-17 17:24:35      阅读:239      评论:0      收藏:0      [点我收藏+]

标签: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)
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(fFive 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! Hows it hello?

 

Python:字符串处理函数

标签:center   name   int   rate   bin   span   let   count   python   

原文地址:https://www.cnblogs.com/kumata/p/9052072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!