test = 'alEx'

print(test.capitalize())  # 首字母大写 其他都变小写

print(test.casefold())  # 变小写 更牛逼

print(test.lower())  # 全部变小写

print(test.center(20, '*'))  # 设置宽度,一共20个位置,将test放中间,其他用*拼接

print(test.count('E', 1, 2))  # test中存在E的数量,从哪开始到哪结束,不填表示从头到尾,左开右闭

print(test.endswith('x'))  # 判断是否以x结尾

print(test.endswith('E', 2, 4))

print(test.startswith('a'))  # 以a开始

test = 'alExalEx'

print(test.find('E'))  # 从前往后找寻找E出现在哪个位置(首次),可以加位置,返回-1代表没找到

print(test.index('E'))  # 未找到就报错

test = 'I am {name}'

print(test.format(name='alex'))  # format格式化

print(test.format_map({'name': 'alex'}))

test = 'afds324353dcz3fads5sd中'

print('*******')
print(test.isalnum()) # 只有数字和字母的时候返回True,汉字也可以 print(test.isalpha()) # 判断是否只是字母 s = 'fasd\t324\twklds' print(s.expandtabs(3)) # 三个一组寻找\t 找到之后剩余的用空格填充 test = '' print(test.isdigit()) # 判断是否只是数字 print(test.isdecimal()) # 是否是数字 有局限 print(test.isnumeric()) test = '_qw12' # 字母 数字 下划线
print(test.isidentifier()) # 判断是否符合标识符 test = 'asdfh\tjfas\n'
# 是否存在不可显示的字符 print(test.isprintable()) test = ' '
print(test.isspace()) # 判断是否是空格 test = 'hello world' print(test.title()) # 转换成标题 print(test.istitle()) # 判断是否是标题 test = '你是风儿我是沙' print(' '.join(test)) # 插入空格 print(test.center(20, '*')) # 设置宽度,一共20个位置,将test放中间,其他用*拼接 test = 'alexA' print(test.ljust(20, '*')) print(test.rjust(20, "*")) print(test.zfill(20)) # 前边用0填充 print(test.lower()) # 转化成小写 print(test.islower()) # 判断是否全部是小写 print(test.upper()) # 转化成大写 print(test.isupper()) # 判断是否是大写 test = ' ale x ' print(test.lstrip()) # 去除左边空格,换行 print(test.rstrip()) # 去除右边空格 print(test.strip()) # 去除两边空格 test = 'alex'
print(test.lstrip('a')) # 去除a(以a开头) v = 'aeiuo'
m = str.maketrans('aeiou', '')
print(v.translate(m)) # 替换 test = 'alexafdsfffsiensfls' print(test.partition('s')) # 以s做分割 print(test.rpartition('s')) print(test.split('s')) # 分割 print(test.rsplit('s')) test = 'afsd\nfda' print(test.splitlines()) # 根据换行分割
print('name', 'alex', 'age', '18', sep=':')  # 字符串拼接

import string


values = {'var': 'foo'}

t = string.Template('''
Variable: $var
Escape: $$
Variable in text: ${var}iable
''')
print('TEMPLATE:', t.substitute(values)) s = '''
Variable: %(var)s
Escape: %%
Variable in text: %(var)siable
'''
print('INTERPOLATION', s % values) s = '''
Variable: {var}
Escape: {{}}
Variable in text: {var}iable
'''
print('FORMAT', s.format(**values)) t = string.Template('$var')
print(t.pattern.pattern)

python字符串魔法的更多相关文章

  1. Python字符串魔法方法

    isalpha()判断是否权威字母或者汉字 isdecimal()(十进制小数)    isdigit()  判断是否为数字 特殊数字 isdigit()判断更为厉害   也是判断数字  都返回Tru ...

  2. 关于python字符串基本操作

    python字符串基本操作,比如字符串的替换.删除.截取.复制.连接.分割等.都是一些关于字符串的一些方法.下面来列举一些,相信对学习python还是有些帮助的. 1.去除空格--strp(): &g ...

  3. python之魔法方法介绍

    1.1. 简介 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加“魔法”的特殊方法. 它们经常是两个下划线包围来命名的(比如 __init__ , __lt__ ) ...

  4. gj3 Python数据模型(魔法函数)

    3.1 什么是魔法函数 类里面,实现某些特性的内置函数,类似 def __xx__(): 的形式. 不要自己定义XX,并不是和某个类挂钩的 class Company(object): def __i ...

  5. Python 简明教程 --- 8,Python 字符串函数

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 好代码本身就是最好的文档.当你需要添加一个注释时,你应该考虑如何修改代码才能不需要注释. -- St ...

  6. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  7. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  8. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  9. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

随机推荐

  1. 用Docker启动php-5.6 fpm服务配合宿主机nginx运行php测试环境

    因为Ubuntu18.04默认的openssl版本就是1.1.0, 而PHP5.6无法在openssl 1.1下编译 "PHP 5.6 is receiving only security ...

  2. PhoneGap-Android-HTML5-WebSocket

    https://github.com/FreakDev/PhoneGap-Android-HTML5-WebSocket

  3. 安装astrixx firefox插件

    以上步骤适用于firefox 45.0.1. 将astrixx的firefox插件下载到本地,这个插件的下载地址很难找...全名是astrixx proxy switcher about:config ...

  4. 【Python】将python3.6软件的py文件打包成exe程序

    下载pyinstaller pyinstaller 改变图标 pyinstaller -F --icon=my.ico xxx.py 采用命令行操作的办法 在cmd命令行中,输入代码: 首先,前往Py ...

  5. Spring自动扫描无法扫描jar包中bean的解决方法(转)

    转载自:http://www.jb51.net/article/116357.htm 在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容.然而,最近对公司的公共模块进行整理发布后.sprin ...

  6. URLEncoder 和URLDecoder

    通常在字符串的编码转换上,可以使用这两个类: public static void main(String[] args) { String str = "你好吗?我很好!"; t ...

  7. (实用)CentOS 6.3更新内置Python2.6

    在安装Kilo版的OpenStack时,我们发现社区已经将Python升到2.7,而CentOS 6.3上仍然在使用2.6版的Python.本文记录将CentOS 6.3内置的Python2.6更新为 ...

  8. CentOS最小化安装(一)

    一.配置网络 切记记得配置DNS,否则Ping不通 在目录中进行网络配置文件的查找:  /etc/sysconfig/network-scripts/ 1   1 /etc/sysconfig/net ...

  9. Java 读取ANSI文件中文乱码问题解决方式[转]

    第一步:首先判断源文件的编码格式: 按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以,基本的原理就是只要读出文件前三个字节,判定这些字节的值,就可以得知其编码的格式. ...

  10. last

    last reboot 显示系统最后重启的历史记录 last -w 显示自系统启动以来,所有登录的用户(全名)