python初学day2--(字符串(str)内部功能简介)

Str内部功能简介

1,pitalize(self): 将字符串首字母变成大写

s = ‘hello‘

result = s.capitalize()

print(result)              结果: Hello

2,casefold(self): 见字符串变成小写

s = ‘HELLO‘

result = s.casefold()

print(result)      结果:hello

3,center(self, width, fillchar=None):经字符串居中,默认用空格填充

s = ‘HELLO‘

result = s.center(20,‘*‘)

print(result)     结果: *******HELLO********

4, count(self, sub, start=None, end=None): 统计一个字符在字符串中出现的次数,并且可以指定起,末位置。

s = ‘HELLO‘

result = s.count(‘L‘)

print(result)      结果: 2

5, endswith(self, suffix, start=None, end=None):判断一个字符串是否已某个字符或者字符串结尾, 并且可以指定起,末位置。

s = ‘HELLO‘

result = s.endswith(‘LO‘)

result1 = s.endswith(‘LX‘)

print(result)    结果:True

print(result1)   结果:False

6, expandtabs(self, tabsize=8):将字符串中的tab键用空格代替,默认8个空格

s = ‘HELLO\tWORLD!‘

result = s.expandtabs()

result1 = s.expandtabs(20)

print(result)     结果: HELLO   WORLD!

print(result1)    结果: HELLO               WORLD!

7, find(self, sub, start=None, end=None): 在一个字符串中查找一个字符或者字符串,如果找到返回下标,如果找不到返回-1

s = ‘HELLOWORLDHELLO!‘

result = s.find(‘LO‘)

result1 = s.find(‘LX‘) print(result)    结果:3

print(result1)   结果:-1

8, index(self, sub, start=None, end=None): 在一个字符串中查找一个字符或者字符串,如果找到返回下标,如果找不到抛异常(报错)

s = ‘HELLOWORLDHELLO!‘

result = s.index(‘LO‘)

print(result)     结果:3

s = ‘HELLOWORLDHELLO!‘

result1 = s.index(‘LX‘)

print(result1)    如下结果:  报错内容

Traceback (most recent call last):       File "C:/python/test2.py", line 3, in <module>

result1 = s.index(‘LX‘)

ValueError: substring not found

9, format(self, *args, **kwargs):字符串格式化

s = ‘flowers‘

print(‘This is {} !‘.format(s))

print(‘This is {0} flowers, That is {1} flowers‘.format(‘red‘,‘yellow‘))

print(‘Two colour {name} and {name1}‘.format(name1 = ‘yellow‘, name = ‘red‘))

结果:

This is flowers !

This is red flowers, That is yellow flowers

Two colour red and yellow

10, isalnum(self):判断一个字符串是否仅是由数字和字母组成

s = ‘flowers12‘

s1 = ‘flowers12!!‘

result = s.isalnum()

result1 = s1.isalnum()

print(result)    结果: True

print(result1)   结果: False

11, isalpha(self): 判断一个字符串是否仅由字母组成

s = ‘flowers‘

s1 = ‘flowers12‘

result = s.isalpha()

result1 = s1.isalpha()

print(result)     结果: True

print(result1)    结果: False

12, isdigit(self):判断一个字符串是否是数字:

s = ‘1223‘

s1 = ‘flowers12‘

result = s.isdigit()

result1 = s1.isdigit()

print(result)     结果: True

print(result1)    结果: False

13, isdecimal(self):判断一个字符串是否是十进制数

s = ‘1223‘

s1 = ‘0x1223‘

result = s.isdecimal()

result1 = s1.isdecimal()

print(result)     结果: True

print(result1)    结果: False

14, isidentifier(self):判断一个标识符是否合法

s = ‘12hello‘

s1 = ‘hello‘

result = s.isidentifier()

result1 = s1.isidentifier()

print(result)     结果: False

print(result1)    结果: True

15, islower(self): 判断一个字符串是否是小写。

s = ‘Hello‘

s1 = ‘hello‘

result = s.islower()

result1 = s1.islower()

print(result)     结果: False

print(result1)    结果: True

16, isidentifier(self):判断一个字符串是否只有数字

s = ‘1256‘

s1 = ‘test1256‘

result = s.isnumeric()

result1 = s1.isnumeric()

print(result)     结果: True

print(result1)    结果: False

17,isspace(self): 判断字符串是否是空格

s  = ‘  ‘

s1 = ‘test  1256‘

result = s.isspace()

result1 = s1.isspace()

print(result)     结果: True

print(result1)    结果: False

18,istitle(self):判断一个字符串是否为标题

s = ‘Hello World‘

s1 = ‘hello world‘

result = s.istitle()

result1 = s1.istitle()

print(result)     结果: True

print(result1)    结果: False

19,isupper(self):判断字符串是否为大写

s = ‘HELLO‘

s1 = ‘Hello‘

result = s.isupper()

result1 = s1.isupper()

print(result)     结果: True

print(result1)    结果: False

20,join(self, iterable):用字符串将参数(interable)分割

s = ‘HELLO‘

result = s.join(‘xxx**‘)

print(result)     结果: xHELLOxHELLOxHELLO*HELLO*

21, ljust(self, width, fillchar=None) and rjust(self, width, fillchar=None)      从左或者从右用指定字符填充指定宽度

s = ‘HELLO‘

result = s.ljust(12,‘*‘)

result1 = s.rjust(12,‘*‘)

print(result)     结果: HELLO*******

print(result1)    结果: *******HELLO

21, lower(self) and upper(self):将字符串变成小写 and 变成大写

s = ‘HELLOworld‘

result = s.lower()

result1 = s.upper()

print(result)     结果: helloworld

print(result1)    结果: HELLOWORLD

22, lstrip(self, chars=None) and  rstrip(self, chars=None):从左边或者右边移除指定的字符

s = ‘HELLOworld‘

result = s.lstrip(‘HE‘)

result1 = s.rstrip(‘ld‘)

print(result )      结果: LLOworld

print(result1)     结果: HELLOwor

23,partition(self, sep): 将一个字符串按照指定的字符分割成一个元组

s = ‘HELLOworld‘

result = s.partition(‘wo‘)

result1 = s.partition(‘ss‘)

print(result)     结果: (‘HELLO‘, ‘wo‘, ‘rld‘)

print(result1)    结果: (‘HELLOworld‘, ‘‘, ‘‘)

24, replace(self, old, new, count=None):用新的字符替代老的字符,count指定替代个数,默认全部替代

s = ‘hello word door‘

result = s.replace(‘o‘,‘T‘)

result1 = s.replace(‘o‘,‘T‘,2)

print(result)      结果: hellT wTrd dTTr

print(result1)     结果: hellT wTrd door

25,split(self, sep=None, maxsplit=-1): 将一个字符串用指点的字符分割成一个列表,指定的字符自动去除。

s = ‘helloworddoor‘

result = s.split(‘o‘)

print(result)        结果: [‘hell‘, ‘w‘, ‘rdd‘, ‘‘, ‘r‘]

26,splitlines(self, keepends=None):,将一个字符串按照行分割成一个列表

s = ‘‘‘

this is a cat

that is a dog

‘‘‘

result = s.splitlines()

print(result)     结果: [‘‘, ‘this is a cat‘, ‘that is a dog‘, ‘‘]

27,startswith(self, prefix, start=None, end=None): 判断一个字符串是否以指导的字符结尾。

s = ‘helloworld‘

result = s.startswith(‘he‘)

result1 = s.startswith(‘el‘)

print(result)        结果: True

print(result1)      结果: False

28, swapcase(self):将一个字符串大小写转换

s = ‘HELLOworld‘

result = s.swapcase()

print(result)     结果: helloWORLD

29,title(self):将一个字符串转化为标题

s = ‘this is a buautiful flowers‘

result = s.title()

print(result)     结果: This Is A Buautiful Flowers

30,maketrans(self, *args, **kwargs) and maketrans(self, *args, **kwargs):    用映射的形势替换对应字符,最后一个参数是去除该字符。

原文地址:https://www.cnblogs.com/python-exercise/p/8167907.html

时间: 2024-08-08 17:21:28

python初学day2--(字符串(str)内部功能简介)的相关文章

Python Str内部功能-个人课堂笔记,课后总结

查看str内部功能,可通过和int一样的方法,或者使用type(str).dir(str)来查看. 1.capitalize:首字母大写 S.capitalize() -> string str1 = "parr" result = str1.capitalize() print(result) #返回Parr 2.center(self, width, fillchar=None):内容居中,width:总长度:fillchar:空白处填充内容(默认无) S.center(wi

Python str内部功能介绍

def capitalize(self): str = 'aGe'print(str.capitalize())结果:Age结论:首字母大写,其他字母都小写 def casefold(self): str = 'AGE-age'print(str.casefold()) 结果:age-age结论:首字母大写,其他字母都小写 def center(self, width, fillchar=None): str = 'AGE-age'print(str.center(20,'='))结果:====

mssql 系统函数 字符串函数 space 功能简介

转自: http://www.maomao365.com/?p=4672  一.space 函数功能简介 space功能:返回指定数量的空格参数简介: 参数1: 指定数量,参数需为int类型 注意事项: 1 如果参数1输入为非varchar或nvarchar类型,那么sql引擎先进行类型转换,如果转换失败,则返回错误信息 否则继续执行此函数 2 如果参数等于零的数值,那么就返回空字符串 3 如果参数小于零,那么就返回null ,会导致字符串叠加操作失败  二.space 函数举例说明例1: /*

python里float和long内部功能及字符串str介绍

1 float类型 2 as_integer_ratio #把小数转换为最简比 3 列如 4 0.5转换为1/2,不能转换2/4,因为是最简比 5 其它以及long类型与int类型一样 6 7 无论哪一门语音基本都是对字符串与集合做操作,学的时候,可以先学集合和字符串的使用方法,列如python的集合,列表,字典,元组等 8 9 str字符串的操作,重点 10 type,查看类型,对象是由那个类创建的 11 列如: 12 type(name) 13 dir 查看类里所有的方法,提供了哪些成员可以

python初学day3 --(list,tuple,dict,set 内部功能简介)

list 内部方法汇总 1,def append(self, p_object): 对原列表添加元素 li = list((2,3,4,6,9,7)) li.append('a') print(li)                        结果:[2, 3, 4, 6, 9, 7, 'a'] 2,def clear(self):清除列表中的所有元素 li = list((2,3,4,6,9,7)) li.clear() print(li)                        

Python基础语法--字符串内置功能

1.capitalize---首字母转变成大写,如果首子母已经是大写则还是输出首字母大写结果 例子: 2.casefold ---大写转小写针对所有内容 例子: 3.center ----内容居中 例子:    ps  该例子输出20个字符(包含字符串"admin所占用的5个字符"),并把字符串"admin"居中 4.count ---统计出现的次数,返回一个正整数类型 例子: ps 结果1:是指定从第4个字符串到第9个字符串中统计(字符串从位置0开始计算) 5.e

09 python初学 (字符串)

# 重复输出字符串 print('hello' * 2) # >>>hellohello # 字符串切片操作 print('hello'[2:]) # >>>llo # 关键字 in print('ll' in 'hello') # >>> True # 字符串拼接 # 不推荐使用此种方式,方式一 a = '123' b = 'abc' c = 'haha' d = a + b print(d) # >>> 123abc # 方式二j

python学习day2(二)

1.类与对象的关系 对于Python,一切事物都是对象,对象基于类创建 type是获取类的 dir是获取这个类里面的成员 2.int内部功能介绍 bit_length:返回表示当前数字占用的最少位数: conjugate:返回复数的共轭复数 __abs__:返回绝对值(或abs(-11)) __add__:相加(+) __and__:与运算 __cmp__:比较两个数大小(3.x中取消) __bool__:转换为布尔值 __divmod__:相除,得到商和余数组成的元祖 __eq__:  等于(

istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string

 1. istringstream字符串流 #include <iostream> #include <sstream> #include <string> using namespace std; struct MyStruct { string str1, str2, str3; double db; int num; char ch; }; void main() { string  mystring("china  google microsoft