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 ""
功能:将指定字符串用指定新字符串代替。
相关文章推荐
- 菜鸟的Python之路--基础知识(字符串的基本操作)
- Python基础教程代码与注释P44 3.1 基本字符串操作 3.2 字符串格式化:精简版
- Python基础教程 基本的数值操作,字符串操作。
- python基础知识 (7) ----- 字符串的基本操作
- python基础--字符串基本操作
- 【Python】2.字符串、列表、元组、字典、集合的基本操作
- 转--Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python基础===字符串的制表,换行基础操作
- Python学习笔记(二):基本数据类型及操作(逻辑、字符串、浮点、复数)
- python字符串的基本操作
- Python基础-文件的基本操作
- Python基础:字符串的操作&字符串内建函数
- Python字符串的基本操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 四十八、基础框架(六)不可修改字符串2-其它基本字符串操作
- python基础: 字符串操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python入门基础(2)——基本类型与字符串处理
- python学习笔记之基础操作(五)字符串格式化(2)format
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结