Python开发(基础):字符串
字符串常用方法说明
#!/usr/bin/env python
# -*- coding:utf-8 -*-# class str(basestring):
# """
# str(object='') -> string
#
# Return a nice string representation of the object.
# If the argument is a string, the return value is the same object.
# """
#
# def capitalize(self): # real signature unknown; restored from __doc__
# """
# 首字母大写
# S.capitalize() -> string
#
# Return a copy of the string S with only its first character
# capitalized.
# """
# return ""
#
str_capitalize = 'welcome'
print str_capitalize.capitalize()
#输出:Welcome# def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
# """
# 原字符串居中显示,两边可选择填充字符
# S.center(width[, fillchar]) -> string
#
# Return S centered in a string of length width. Padding is
# done using the specified fill character (default is a space)
# """
# return ""
str_center = 'hello'
print str_center.center(20,'*')
#输出:*******hello********
# 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
#
str_count = 'helikdk;a'
print str_count.count('k')
#输出:2
# def decode(self, encoding=None, errors=None): # real signature unknown; restored from __doc__
# """
# 字符串解码
# S.decode([encoding[,errors]]) -> object
#
# Decodes S using the codec registered for encoding. encoding defaults
# to the default encoding. errors may be given to set a different error
# handling scheme. Default is 'strict' meaning that encoding errors raise
# a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
# as well as any other name registered with codecs.register_error that is
# able to handle UnicodeDecodeErrors.
# """
# return object()
#
# def encode(self, encoding=None, errors=None): # real signature unknown; restored from __doc__
# """
# 字符串编码
# S.encode([encoding[,errors]]) -> object
#
# Encodes S using the codec registered for encoding. encoding defaults
# to the default encoding. errors may be given to set a different error
# handling scheme. Default is 'strict' meaning that encoding errors raise
# a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
# 'xmlcharrefreplace' as well as any other name registered with
# codecs.register_error that is able to handle UnicodeEncodeErrors.
# """
# return object()
#
# def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 判断字符串是否以什么结尾
# S.endswith(suffix[, start[, end]]) -> bool
#
# Return True if S ends with the specified suffix, False otherwise.
# With optional start, test S beginning at that position.
# With optional end, stop comparing S at that position.
# suffix can also be a tuple of strings to try.
# """
# return False
#
# def expandtabs(self, tabsize=None): # real signature unknown; restored from __doc__
# """
# 将字符串中的tab键替换成空格(默认是8个空格),也可自己指定
# S.expandtabs([tabsize]) -> string
#
# Return a copy of S where all tab characters are expanded using spaces.
# If tabsize is not given, a tab size of 8 characters is assumed.
# """
# return ""
#
str_expandtabs = 'hello\talex'
print str_expandtabs
print str_expandtabs.expandtabs(20)
#输出:hello alex
# hello alex
# def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 查找字符串中子字符串第一次出现的位置(下标从0开始),也可指定从某个位置开始查找,或结束查找,
# 如果找不到就返回-1
# 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
#
str_find = 'kdlafjdklkdla'
print str_find.find('la',4)
#输出:11
# def format(self, *args, **kwargs): # known special case of str.format
# """
# 通过通配符来格式化字符串
# S.format(*args, **kwargs) -> string
#
# Return a formatted version of S, using substitutions from args and kwargs.
# The substitutions are identified by braces ('{' and '}').
# """
# pass
#
str_format = 'hell {0},age {1}'
print str_format.format('alex',19)
#输出:hell alex,age 19
# def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 查找字符串中子字符串第一次出现的位置(下标从0开始),也可指定从某个位置开始查找,或结束查找,找不到会报错,功能同find类似
# S.index(sub [,start [,end]]) -> int
#
# Like S.find() but raise ValueError when the substring is not found.
# """
# return 0
str_index = 'kd;fjdkl;sfjkd;akd'
print str_index.index('fj')
#输出:3# 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
#
str_isalnum = 'dafdfdk;j123'
print str_isalnum.isalnum()
#输出:False
# 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
#
# 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
#
# def islower(self): # real signature unknown; restored from __doc__
# """
# 判断是否全小写
# S.islower() -> bool
#
# Return True if all cased characters in S are lowercase and there is
# at least one cased character in S, False otherwise.
# """
# return False
#
# def isspace(self): # real signature unknown; restored from __doc__
# """
# 判断字符串是否为空(包括空格)
# S.isspace() -> bool
#
# Return True if all characters in S are whitespace
# and there is at least one character in S, False otherwise.
# """
# return False
#
# def istitle(self): # real signature unknown; restored from __doc__
# """# 判断是否标题(即:首字母大写,其他全部小写)
# S.istitle() -> bool
#
# Return True if S is a titlecased string and there is at least one
# character in S, i.e. uppercase characters may only follow uncased
# characters and lowercase characters only cased ones. Return False
# otherwise.
# """
# return False
#
# def isupper(self): # real signature unknown; restored from __doc__
# """
# 判断是否全大写
# S.isupper() -> bool
#
# Return True if all cased characters in S are uppercase and there is
# at least one cased character in S, False otherwise.
# """
# return False
#
# def join(self, iterable): # real signature unknown; restored from __doc__
# """
# 将list、元组等通过一个字符或字符串连接成一个字符串
# S.join(iterable) -> string
#
# Return a string which is the concatenation of the strings in the
# iterable. The separator between elements is S.
# """
# return ""
#
li = ['alex','age']
print '.'.join(li)
#输出:alex.age
# def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
# """
# 左对齐,右边填充所给的字符
# S.ljust(width[, fillchar]) -> string
#
# Return S left-justified in a string of length width. Padding is
# done using the specified fill character (default is a space).
# """
# return ""
#
str_ljust = ' hello,where are you from ? ]'
print str_ljust.ljust(40,'*')
print str_ljust.ljust(50,"*")
#输出:
# hello,where are you from ? ]*******
# hello,where are you from ? ]*****************
# def lower(self): # real signature unknown; restored from __doc__
# """
# 将字符串转换为小写
# S.lower() -> string
#
# Return a copy of the string S converted to lowercase.
# """
# return ""
#
# def lstrip(self, chars=None): # real signature unknown; restored from __doc__
# """
# 去掉字符串左边的空格
# S.lstrip([chars]) -> string or unicode
#
# Return a copy of the string S with leading whitespace removed.
# If chars is given and not None, remove characters in chars instead.
# If chars is unicode, S will be converted to unicode before stripping
# """
# return ""
#
print ' dkla; fda; '.lstrip()
# def partition(self, sep): # real signature unknown; restored from __doc__
# """
# 根据的所级字符(或字符串),将原字符串拆分成三个部分组成一个元组,如果所给字符在字符串中找不到,返回的元祖由两个空字符串和原字符串本身组成
# S.partition(sep) -> (head, sep, tail)
#
# Search for the separator sep in S, and return the part before it,
# the separator itself, and the part after it. If the separator is not
# found, return S and two empty strings.
# """
# pass
#
str_partition = 'headseptail'
print str_partition.partition('sep')
#输出:('head', 'sep', 'tail')
# def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
# """
# 替换字符串中的某个部份,可经指定替换几次
# S.replace(old, new[, count]) -> string
#
# Return a copy of string 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 ""
#
# def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 字符串中查找某个字符第一次出现的位置,从右边开始,用法同find(默认从左边开始)
# 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
#
print 'dklfkd;fjdklsa'.rfind('dk')
#输出:9
# def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 字符串中查找某个字符第一次出现的位置,从右边开始,用法同index(默认从左边开始)
# S.rindex(sub [,start [,end]]) -> int
#
# Like S.rfind() but raise ValueError when the substring is not found.
# """
# return 0
#
# def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
# """
# 右对齐,左边填充所给的字符
# S.rjust(width[, fillchar]) -> string
#
# Return S right-justified in a string of length width. Padding is
# done using the specified fill character (default is a space)
# """
# return ""
#
print ' kfdls;k kldjklfdlk '.rjust(50,'*')
#输出:************************ kfdls;k kldjklfdlk
# def rpartition(self, sep): # real signature unknown; restored from __doc__
# """
# 用法同partition ,只是从右边开始
# S.rpartition(sep) -> (head, sep, tail)
#
# Search for the separator sep in S, starting at the end of S, and return
# the part before it, the separator itself, and the part after it. If the
# separator is not found, return two empty strings and S.
# """
# pass
#
# def rsplit(self, sep=None, maxsplit=None): # real signature unknown; restored from __doc__
# """
# 用法同split
# S.rsplit([sep [,maxsplit]]) -> list of strings
#
# Return a list of the words in the string S, using sep as the
# delimiter string, starting at the end of the string and working
# to the front. If maxsplit is given, at most maxsplit splits are
# done. If sep is not specified or is None, any whitespace string
# is a separator.
# """
# return []
s_rsplit = 'dkls;jfkdlak;jkdls;af'
print s_rsplit.rsplit(';')
#输出:['dkls', 'jfkdlak', 'jkdls', 'af']
# def rstrip(self, chars=None): # real signature unknown; restored from __doc__
# """
# 去掉字符串右边的空格
# S.rstrip([chars]) -> string or unicode
#
# Return a copy of the string S with trailing whitespace removed.
# If chars is given and not None, remove characters in chars instead.
# If chars is unicode, S will be converted to unicode before stripping
# """
# return ""
#
# def split(self, sep=None, maxsplit=None): # real signature unknown; restored from __doc__
# """
# 要据所给字符串字符串拆分成一个list
# S.split([sep [,maxsplit]]) -> list of strings
#
# Return a list of the words in the string S, using sep as the
# delimiter string. If maxsplit is given, at most maxsplit
# splits are done. If sep is not specified or is None, any
# whitespace string is a separator and empty strings are removed
# from the result.
# """
# return []
#
# def splitlines(self, keepends=False): # real signature unknown; restored from __doc__
# """
# S.splitlines(keepends=False) -> list of strings
#
# Return a list of the lines in S, breaking at line boundaries.
# Line breaks are not included in the resulting list unless keepends
# is given and true.
# """
# return []
#
# def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
# """
# 判断所给字符串是否以所给字符(或字符串)开头
# S.startswith(prefix[, start[, end]]) -> bool
#
# Return True if S starts with the specified prefix, False otherwise.
# With optional start, test S beginning at that position.
# With optional end, stop comparing S at that position.
# prefix can also be a tuple of strings to try.
# """
# return False
#
# def strip(self, chars=None): # real signature unknown; restored from __doc__
# """
# 去掉字符串左右两边的窗格
# S.strip([chars]) -> string or unicode
#
# Return a copy of the string S with leading and trailing
# whitespace removed.
# If chars is given and not None, remove characters in chars instead.
# If chars is unicode, S will be converted to unicode before stripping
# """
# return ""
#
print ' dksl klds '.strip()
#输出:dksl klds
# def swapcase(self): # real signature unknown; restored from __doc__
# """
# 大小写转换,大写变小写,小写变大写
# S.swapcase() -> string
#
# Return a copy of the string S with uppercase characters
# converted to lowercase and vice versa.
# """
# return ""
#
print 'AkdlNkdlKDKLda;'.swapcase()
#输出:aKDLnKDLkdklDA;
# def title(self): # real signature unknown; restored from __doc__
# """
# 字符串首字母大写,其他全部转为小写
# S.title() -> string
#
# Return a titlecased version of S, i.e. words start with uppercase
# characters, all remaining cased characters have lowercase.
# """
# return ""
#
print 'kdlsaKDlk'.title()
#输出:Kdlsakdlk
# def translate(self, table, deletechars=None): # real signature unknown; restored from __doc__
# """
# S.translate(table [,deletechars]) -> string
#
# Return a copy of the string S, where all characters occurring
# in the optional argument deletechars are removed, and the
# remaining characters have been mapped through the given
# translation table, which must be a string of length 256 or None.
# If the table argument is None, no translation is applied and
# the operation simply removes the characters in deletechars.
# """
# return ""
#
# def upper(self): # real signature unknown; restored from __doc__
# """
# 字符串全部转为大写
# S.upper() -> string
#
# Return a copy of the string S converted to uppercase.
# """
# return ""
#
# def zfill(self, width): # real signature unknown; restored from __doc__
# """
# 根据所给宽度,将字符串左边不够的位置上填充0
# S.zfill(width) -> string
#
# Pad a numeric string S with zeros on the left, to fill a field
# of the specified width. The string S is never truncated.
# """
# return ""
print '1000'.zfill(8)
#输出:00001000
# def _formatter_field_name_split(self, *args, **kwargs): # real signature unknown
# pass
#
# def _formatter_parser(self, *args, **kwargs): # real signature unknown
# pass
#
# def __add__(self, y): # real signature unknown; restored from __doc__
# """ x.__add__(y) <==> x+y """
# pass
#
# def __contains__(self, y): # real signature unknown; restored from __doc__
# """ x.__contains__(y) <==> y in x """
# pass
#
# def __eq__(self, y): # real signature unknown; restored from __doc__
# """ x.__eq__(y) <==> x==y """
# pass
#
# def __format__(self, format_spec): # real signature unknown; restored from __doc__
# """
# S.__format__(format_spec) -> string
#
# Return a formatted version of S as described by format_spec.
# """
# return ""
#
# def __getattribute__(self, name): # real signature unknown; restored from __doc__
# """ x.__getattribute__('name') <==> x.name """
# pass
#
# def __getitem__(self, y): # real signature unknown; restored from __doc__
# """ x.__getitem__(y) <==> x[y] """
# pass
#
# def __getnewargs__(self, *args, **kwargs): # real signature unknown
# pass
#
# def __getslice__(self, i, j): # real signature unknown; restored from __doc__
# """
# x.__getslice__(i, j) <==> x[i:j]
#
# Use of negative indices is not supported.
# """
# pass
#
# def __ge__(self, y): # real signature unknown; restored from __doc__
# """ x.__ge__(y) <==> x>=y """
# pass
#
# def __gt__(self, y): # real signature unknown; restored from __doc__
# """ x.__gt__(y) <==> x>y """
# pass
#
# def __hash__(self): # real signature unknown; restored from __doc__
# """ x.__hash__() <==> hash(x) """
# pass
#
# def __init__(self, string=''): # known special case of str.__init__
# """
# str(object='') -> string
#
# Return a nice string representation of the object.
# If the argument is a string, the return value is the same object.
# # (copied from class doc)
# """
# pass
#
# def __len__(self): # real signature unknown; restored from __doc__
# """ x.__len__() <==> len(x) """
# pass
#
# def __le__(self, y): # real signature unknown; restored from __doc__
# """ x.__le__(y) <==> x<=y """
# pass
#
# def __lt__(self, y): # real signature unknown; restored from __doc__
# """ x.__lt__(y) <==> x<y """
# pass
#
# def __mod__(self, y): # real signature unknown; restored from __doc__
# """ x.__mod__(y) <==> x%y """
# pass
#
# def __mul__(self, n): # real signature unknown; restored from __doc__
# """ x.__mul__(n) <==> x*n """
# pass
#
# @staticmethod # known case of __new__
# def __new__(S, *more): # real signature unknown; restored from __doc__
# """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
# pass
#
# def __ne__(self, y): # real signature unknown; restored from __doc__
# """ x.__ne__(y) <==> x!=y """
# pass
#
# def __repr__(self): # real signature unknown; restored from __doc__
# """ x.__repr__() <==> repr(x) """
# pass
#
# def __rmod__(self, y): # real signature unknown; restored from __doc__
# """ x.__rmod__(y) <==> y%x """
# pass
#
# def __rmul__(self, n): # real signature unknown; restored from __doc__
# """ x.__rmul__(n) <==> n*x """
# pass
#
# def __sizeof__(self): # real signature unknown; restored from __doc__
# """ S.__sizeof__() -> size of S in memory, in bytes """
# pass
#
# def __str__(self): # real signature unknown; restored from __doc__
# """ x.__str__() <==> str(x) """
# pass
#
#
# bytes = str
Python开发(基础):字符串相关推荐
- python开发基础戴歆第四章_第一阶段:Python开发基础 day04 课后练习
第一阶段:Python开发基础 day04 课后作业 1.简述python的五大数据类型的作用.定义方式.使用方法 数字类型 字符串类型 列表 字典 布尔型 答:数字类型分为整型(int)和浮点型(f ...
- c语言 dict数据类型,Python开发基础之Python常用的数据类型
Python开发基础之Python常用的数据类型 发布时间:2020-08-02 20:07:19 来源:51CTO 阅读:348 作者:warrent 博文大纲 一.Python介绍 二.Pytho ...
- dockerfile拉取私库镜像_还在用Alpine作为你Docker的Python开发基础镜像?其实Ubuntu更好一点...
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_173 一般情况下,当你想为你的Python开发环境选择一个基础镜像时,大多数人都会选择Alpine,为什么?因为它太小了,仅仅只有 ...
- [转载] python 语言基础 - 字符串常用函数及操作
参考链接: Python字符串| ljust rjust center python为字符串操作提供了很多常用的内建函数,让我们在操作字符串时能够更加简单方便. 下面以某个字符串为例来一一介绍一下 现 ...
- Python开发基础总结之套接字+字符串+正则表达式
一.套接字编程: 函数的功能基本和c类似,唯一不同的地方在于当发生错误时,它不是通过返回值来告知的,而是通过触发异常,所以udp中的bind, recvfrom, sendto必须要进行捕捉异常. 套 ...
- Python开发基础总结之模块+日志+自省
一.模块的使用 1. 如果不想将模块的某些函数和变量被别的模块使用,可以以单下划线开头.这样import 是没有的,但是使用import mode,然后mode.fun仍然可以调用.在class ...
- Python开发基础----异常处理、socket套接字基础1
异常处理 错误 程序里的错误一般分为两种: 1.语法错误,这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 2.逻辑错误,人为造成的错误,如数据类型错误.调用方法错误等,这些解 ...
- python开发基础04-函数、递归、匿名函数、高阶函数、装饰器
匿名函数 lamba lambda x,y,z=1:x+y+z 匿名就是没有名字 def func(x,y,z=1):return x+y+z匿名 lambda x,y,z=1:x+y+z #与函数有 ...
- python开发基础知识
啦啦啦 Python基础知识 面向对象和面向过程的区别 装饰器 Python装饰器@staticmethod和@classmethod区别和使用 深拷贝.浅拷贝和等号赋值 GIL 类 Python内存 ...
- Python开发基础总结之XML+time+OO
一.XML 的使用 处理xml消息包比较好用的模块是xml.etree.ElementTree. Element执行xml的根节点. elem.find(path):查找根节点下面路径为path的子节 ...
最新文章
- 备忘--Sinfors 设备出厂IP
- 职业中专的计算机综合应用,职业中专计算机教学的思考
- python内存管理方法_Python 内存管理大揭秘
- 【常见笔试面试算法题12续集六】动态规划算法案例分析6 最优编辑练习题
- 转:Python中的文件和目录操作
- Jmeter系列之接口依赖
- 微软亚研院 | 智能信息检索综述
- mysql 锁 行级_全局锁、表级锁、行级锁mysql 极客评论笔记
- Catalyst 65xx系列交换机配置(Native IOS)
- 中国孩子的micro:bit:TurnipBit自制小乐器教程实例
- Windows XP 启动NetMeeting
- 抖音快手短视频 影视后期制作工具网址大全
- MT7628学习笔记(13)——ipk软件包编写与应用
- json-server 模拟数据
- 怎么在图片中添加表格?
- ERA5气象数据下载经验分享
- 安卓开发 Fragment的简单使用与解析
- 用C语言计算BMI值
- 基于51单片机的电子万年历设计
- Scratch3.0——助力新进程序员理解程序(案例八、等差数列计算1)
热门文章
- java for循环返回值_Java中的for循环——通过示例学习Java编程(9)
- 金蝶应收应付模块流程_金蝶KIS专业版应收应付的系统亮点功能
- kudu接受kafka消息_Kafka 在车好多
- C语言科学计算器思路,大神教你如何用C语言实现科学计算器
- php每分钟刷新一次的验证码,php如何在进入页面的时候自动刷新一次验证码
- Android 活动与活动间数据传递
- 用Java求一个三位数,该三位数是与其每位数字的阶乘之和
- 手把手教你架构3d游戏引擎pdf_一个在游戏行业摸爬滚打了十几年的人,为何我对这本书情有独钟...
- 小程序json字符串转 json对象 { name :你好} 转成 { name :你好}
- react控制组件的显示或隐藏, 根据state判断元素显示隐藏 , setState不实时生效解决方法