博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python实战之字符串的详细简单练习
阅读量:6265 次
发布时间:2019-06-22

本文共 4939 字,大约阅读时间需要 16 分钟。

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',  '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',   'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',    'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',    'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',    'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',     'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']name = "william"new_name = "WILLLIAM"print(name.capitalize())# William,capitalize首字母变大写print(new_name.casefold())# willliam,casefold 大写全变小写print(name.center(20,"*"))#输出******william*******print(name.count('l'))#2,统计字符串中l的个数print(name.encode())#b'william',将字符串编码成bytes的格式print(name.endswith('m'))#True,检查字符串是否以m结尾print("william\tzhang".expandtabs(6))#william     zhang,将\t转化为长度为6的空格print(name.find('w'))print(name.find('b'))#0;-1,返回找到的第一个值的索引,如果没有则返回-1format:    msg = "my name is {},and age is {}".format('William',20)    print(msg)    # my name is William,and age is 20    msg1 = "my name is {1},and age is {0}".format('William',20)    print(msg1)    # my name is 20,and age is William    msg2 = "my name is {name},and age is {age}".format(age=18,name='William')    print(msg2)    # my name is William,and age is 18    msg3 = "my name is {name},and age is {age}".format_map({
'name':'william','age':18,}) print('*'*20) print(msg3) # my name is william,and age isname = "William"print(name.index('l'))#2,返回字符串l的索引值print(name.isnumeric())#Falseprint(name.isprintable())#Trueprint(name.isspace())#Falseprint(name.istitle())#Trueprint(name.isupper())# Falseprint("|".join(['zhangsan','lisi','wangermazi']))# zhangsan|lisi|wangermaziprint(" ".join(['zhangsan','lisi','wangermazi']))# zhangsan lisi wangermaziouttab = '1'trantab = str.maketrans(intab,outtab)print(trantab)# {53: 49}str = "15135"print(str.translate(trantab))# 11131# 把数字5变成数字1msg = "William is a good boy"print(msg.partition("is"))# ('William ', 'is', ' a good boy')# 用字符串'is'将msg分开msg = "William is a good boy"print(msg.replace('boy','man'))# William is a good man,替换print(msg.swapcase())# wILLIAM IS A GOOD BOY,大小写互换msg = "William is a good boy"print(msg.replace('boy','man'))# William is a good manprint(msg.swapcase())# wILLIAM IS A GOOD BOYprint(msg.zfill(10))# William is a good boyprint(msg.zfill(30))# 000000000William is a good boy,让整个字符串长度为30print(msg.ljust(30,'*'))# William is a good boy*********,左面对其,右面*号补齐print(msg.rjust(30,"*"))# *********William is a good boy,右边对齐,左边*号补齐

 

#The second learning#!/usr/bin/env python3# -*- coding:utf-8 -*-word = "william is a good boy."print(word.capitalize())# William is a good boy.print(word.center(100,"*"))# ***************************************william is a good boy.***************************************print(word.count("i"))# 3print(word.count("i",9))# 0print(word.encode(encoding='utf-8'))# b'william is a good boy.'print(b'william is a good boy.'.decode())# william is a good boy.print(word.endswith("y."))# True# word = "william is a good boy.\t,llll"print(word.expandtabs(9))# william is a good boy.     ,llllprint(word.find("is",11))# -1print(word.find("is"))# 8print(word.index('oo'))# 14,if cant find,raise errorword = "William"print(word.isalnum())# Trueprint(word.islower())# Trueprint(word.isspace())print("*"*20)print(word.istitle())print("---".join(word))# W---i---l---l---i---a---mword = " William is a good boy"print(word.ljust(30,"1"))# William11111111111111111111111print(word.lower())# williamprint(word.lstrip())# Williamprint(word.partition('is'))# (' William ', 'is', ' a good boy')print(word.partition('hahaha'))# (' William is good boy', '', '')print(word.replace('o',"W"))# William is a gWWd bWyprint(word.split('is'))# [' William ', ' a good boy']print(word.split('o',maxsplit=2))# [' William is a g', '', 'd boy']word = '''William is         a good boy'''print(word.splitlines(keepends=True))# ['William is\n', '         a good boy']print(word.splitlines(keepends=False))# ['William is', '         a good boy']print("|"*30)word = "    william  "print(word.startswith('w'))# Trueprint(word.strip())# williamword = "WIlliam"print(word.swapcase())# wILLIAMprint(word.title())print(word.upper())print(word.zfill(20))print("*"*20)x = "william w 1 445"y = "  Alex william"print(x.__add__(y))print(y.__add__(x))print(y.__contains__(x))print(x.__eq__(y))# print(x.__getattribute__("w"))print("*" * 30)print(x.__repr__())

 

转载于:https://www.cnblogs.com/william126/p/7070549.html

你可能感兴趣的文章
js阻止事件冒泡 return false / e.stopPropagation() /e.preventDefault()
查看>>
CSS伪类使用
查看>>
哈佛成功金句
查看>>
iview Table表格单选框互斥
查看>>
leetcode278
查看>>
CodeForces-771D-Bear and Company
查看>>
PAT 1032 Sharing
查看>>
Extjs设置或获取cookie
查看>>
CC2541蓝牙BLE4.0主从透传工程
查看>>
iOS OC中block使用
查看>>
python之路--操作系统介绍,进程的创建
查看>>
markdown语法小结
查看>>
Java Gui 设计模式中的事件监听
查看>>
JavaSE-final关键字
查看>>
python自动化开发-1
查看>>
Remote远程特性的使用
查看>>
orm在django中的简单使用
查看>>
poj 2373 Dividing the Path
查看>>
dplyr 数据操作 常用函数(4)
查看>>
A股实时股票行情,指数行情web API 使用方法
查看>>