您的位置:首页 > 编程语言 > Python开发

Python基础:字符串的基本操作

2019-07-18 13:48 148 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_36609737/article/details/96317728

1. 字符串类

字符串的相关方法在str类里面,
先来看一下这个类的定义:

class str(object):
"""
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
"""

2. 几个常用方法详解

1. capitalize()

def capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> str

Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
"""
return ""

功能:将字符串的第一个字符大写。
注:若第一个字符不是字母(如空格数字),则不做处理。

2. title()

def title(self): # real signature unknown; restored from __doc__
"""
S.title() -> str

Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case.
"""
return ""

功能:将每个单词的首字母大写。

3. upper()

def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str

Return a copy of S converted to uppercase.
"""
return ""

功能:将所有字母大写。

4. index()

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Raises ValueError when the substring is not found.
"""
return 0

功能:1. 获取子字符串在原字符串中的最低下标。
2. 如果没有找到就报错。

5. find()

def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
"""
return 0

功能:1. 获取子字符串在原字符串中的最低下标。
2. 如果没有找到,则返回-1。

6. rfind()

def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rfind(sub[, start[, end]]) -> int

Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
"""
return 0

功能:1. 获取子字符串在原字符串中的最高下标。
2. 如果没有找到,则返回-1。

7. count()

def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are
interpreted as in slice notation.
"""
return 0

功能:统计子字符串在原字符串中的出现次数。

8. isdigit()

def isdigit(self): # real signature unknown; restored from __doc__
"""
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
"""
return False

功能:判断字符串是否由数字组成。

9. isalpha()

def isalpha(self): # real signature unknown; restored from __doc__
"""
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
"""
return False

功能:判断字符串是否由字母组成。

10. isalnum()

def isalnum(self): # real signature unknown; restored from __doc__
"""
S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
"""
return False

功能:判断字符串是否由字母或数字组成。

11. join()

def join(self, iterable): # real signature unknown; restored from __doc__
"""
S.join(iterable) -> str

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.
"""
return ""

功能:将指定字符串加入到迭代器中。

12. lower()

def lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str

Return a copy of the string S converted to lowercase.
"""
return ""

功能:将字符串小写。

13. upper()

def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str

Return a copy of S converted to uppercase.
"""
return ""

功能:将字符串大写。

14. replace()

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.
"""
return ""

功能:将指定字符串用指定新字符串代替。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: