python学习之字符串转换
配置环境:python 3.6 python编辑器:pycharm
代码如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*- def strCase():
"字符串大小写转换"
print("演示字符串大小写转换")
print("演示字符串S赋值为:' ThIs is a PYTHON '")
S = ' ThIs is a PYTHON '
print("大写转换成小写:\tS.lower() \t= %s"%(S.lower()))
print("小写转换成大写:\tS.upper() \t= %s"%(S.upper()))
print("大小写转换:\t\tS.swapcase() \t= %s"%(S.swapcase()))
print("首字母大写:\t\tS.title() \t= %s"%(S.title()))
print('\n') def strFind():
"字符串搜索、替换"
print("演示字符串搜索、替换等")
print("演示字符串S赋值为:' ThIs is a PYTHON '")
S = ' ThIs is a PYTHON '
print("字符串搜索:\t\tS.find('is') \t= %s"%(S.find('is')))
print("字符串统计:\t\tS.count('s') \t= %s"%(S.count('s')))
print("字符串替换:\t\tS.replace('Is','is') = %s"%(S.replace('Is','is')))
print("去左右空格:\t\tS.strip() \t=#%s#"%(S.strip()))
print("去左边空格:\t\tS.lstrip() \t=#%s#"%(S.lstrip()))
print("去右边空格:\t\tS.rstrip() \t=#%s#"%(S.rstrip()))
print('\n') def strSplit():
"字符串分割、组合"
print("演示字符串分割、组合")
print("演示字符串S赋值为:' ThIs is a PYTHON '")
S = ' ThIs is a PYTHON '
print("字符串分割:\t\tS.split() \t= %s"%(S.split()))
print("字符串组合1: '#'.join(['this','is','a','python']) \t= %s"%('#'.join(['this','is','a','python'])))
print("字符串组合2: '$'.join(['this','is','a','python']) \t= %s"%('$'.join(['this','is','a','python'])))
print("字符串组合3: ' '.join(['this','is','a','python']) \t= %s"%(' '.join(['this','is','a','python'])))
print('\n') def strTest():
"字符串测试"
print("演示字符串测试")
print("演示字符串S1赋值为:'abcd'")
S1 = 'abcd'
print("测试S.isalpha() = %s"%(S1.isalpha()))
print("测试S.isdigit() = %s"%(S1.isdigit()))
print("测试S.isspace() = %s"%(S1.isspace()))
print("测试S.islower() = %s"%(S1.islower()))
print("测试S.isupper() = %s"%(S1.isupper()))
print("测试S.istitle() = %s"%(S1.istitle())) if __name__ == '__main__':
strCase()
strFind()
strSplit()
strTest()
python学习之字符串转换的更多相关文章
- python学习之字符串
最近在学习python,随手做些记录,方便以后回顾 #字符串是不可再改变的序列aa='abcd'#aa[2:]='ff' #报错,不可直接赋值#字符串格式化:使用格式化操作符即百分号%来实现print ...
- Python学习笔记-字符串
Python之使用字符串 1.所有的标准序列操作(索引,分片,乘法,判断成员资格,求长度,取最小值,最大值)对字符串同样适用.但是字符串都是不可变的. 2.字符串格式化使用字符串格式化操作符即%. f ...
- python 数字和字符串转换问题
一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...
- python学习之字符串(上)
字符串python 的字符串被划归为不可变序列这一类别,意味着这些字符串所包含的字符存在从左至右的位置顺序,并且他们不可以在原处修改. 字符串常量单引号 'spam'双引号 "spam& ...
- python学习之字符串常用方法和格式化字符串
Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...
- python :eval将字符串转换成字典
#将字符串打印成字典 b=''' {'record': {'weight':20,'server':'100.1.7.9','maxconn':50},'backend': 'www.oldboy.o ...
- python学习笔记——字符串
类方法string.upper(str)需要引入string模块,实例方法str.upper()不需要引入string模块 无与伦比的列表解析功能 # coding=utf-8 # 列表解析 prin ...
- Python学习之--字符串的使用
一.大小写转换 1. 首字母大写:title(); 如下: 2. 大写转换:upper(),如 3.小写转换:lower(),如 二.合并(拼接)字符串:”+“ 1. 2. 三.制表符.换行 制表符 ...
- Python学习之字符串函数
下面是在看python核心编程中序列字符串中提到的一些函数,根据自己的学习理解总结了下,方便日后用到的时候查看. 1.string.capitalize() 把字符串的第一个字符大写 例子: ...
随机推荐
- vueHistory 模式下,布置到服务器上路由刷新会报nginx404错误
之前写完vue项目后,布置到服务器,用nginx反向代理后,一开始进去,进各种路由都是没问题的,但是一旦f5刷新后就会出现一个nginx404的错误. 经过翻阅vue文档后,发现这是vueHistor ...
- css透明边框实现
代码: css: .box1{ background: black; padding:20px; } .box2{ background: #fff; border: 10px soli ...
- The fool doth think he is wise, but the wise man knows himself to be a fool.
The fool doth think he is wise, but the wise man knows himself to be a fool.愚者总自以为聪明,智者则有自知之明.
- Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.
AndroidStudio升级到3.1后编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plug ...
- Java -GUI开发九九乘法表
Java GUI开发九九乘法表 (1)实现目标: 利用java自带的awt包,基础控件开发一个九九乘法表,点击可以显示对应的乘法口诀. (2)控件选择: 点击——Button 显示——TextFiel ...
- Eclipse:很不错的插件-devStyle,将你的eclipse变成idea风格
使用教程 https://blog.csdn.net/stillonmyway/article/details/79109741 我使用使用的是护眼型的
- WebRTC协议
webrtc协议介绍 MDN webrtc协议 ICE 交互式连接建立Interactive Connectivity Establishment (ICE) 是一个允许你的浏览器和对端浏览器建立连接 ...
- Linux CentOS下部署Java Web项目
本文讲解如何在Linux CentOS下部署Java Web项目的步骤. 一.环境准备: (1)Linux CentOS (2)apache-tomcat-9.0.10 (3)XShell 二.启动t ...
- python3 爬虫笔记(一)beautiful_soup
很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...
- 建堆复杂度O(n)证明
堆排序中首先需要做的就是建堆,广为人知的是建堆复杂度才O(n),它的证明过程涉及到高等数学中的级数或者概率论,不过证明整体来讲是比较易懂的. 堆排过程 代码如下 void print(vector&l ...