PythonNewHere
来源:互联网 发布:三防漆涂覆机编程 编辑:程序博客网 时间:2024/05/19 04:53
第一部分代码:https://github.com/Fu4ng/PythonNewHere/blob/master/L01.py
第二部分关于字符串操作的代码:
https://github.com/Fu4ng/PythonNewHere/blob/master/L03.py
1.各类“字符串”
字符串
# 字符串x = "hello world"y = "let's go "print(y + x)print('"hello world",she said')print(repr("hello world"))print(repr(100000))print(str("hello world"))# 用repr函数会比str多出一个引号# str和 int long 一样,是一种类型。而repr仅仅是函数
input和raw_input两个函数的关系请看我写的这篇小笔记:
http://blog.csdn.net/junloin/article/details/75115775
#input 和 raw_inputname = input("what is your name?")print(name)number =input("what is your number ")print(number)
长字符串
使用三个引号(单双引号都可以)能换行
# 长字符串print('111111111111111111111111111' '111111111111111111111111')print('''2222222222用三个引号能换行222 2222222222222''')
原始字符串
正常打印字符串时,print遇到一些转义符就会执行对应的功能,如下,我们需要打印一个路径,但是路径中的\n是一个转义符。在pycharm看起来很明显,转义符的优先级高于字符串,所以遇到这种情况,我们需要使用原始字符串
#原始字符串print('C:\nowhere') #这个是非原始字符串,因为检查到有\n换行符存在,输出时会换行print (r'C:\nowHere') #原始字符串以r开头,可以存放\,但是原始字符串最后一个字符不能是\# 如果有硬性要求需要最后一个字符必须为\ ,那么我们应该对\进行转义,如下print(r'C:Program Files\foo\bar' '\\')
Unicode字符串
#Unicodeprint(u"hello world")
模版字符串
#模版字符串from string import Templates = Template ("$x,Python $x!")print(s.substitute(x = 'wow'))#替换字段是单词的一部分,那么参数名就必须用括号扩起来,从而指明结尾s = Template("It's ${x}thon")print(s.substitute(x = 'Py'))
2.字符串操作
字符串格式化
format1 = 'hello,%s,%s nough for ya?'values = ('world','hot')print(format1 % values)
hello,world,hot nough for ya?
1.%字符,标记转换说明符的开始
2.转换标记(可选):-左对齐;+表示在转换值之前要加上正负号;“ ”(空白字符)表示在正数前保留空格,0表示转换值若位数不够则用0填充
3.最小字段宽度(可选):转换之后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组读出
4.点(.)后跟精度值(可选):如果转换的是实数,精度值就表现出在小数点后的位数,如果转换的是字符串。那么该数字就表示最大字符宽度,如果是*,那么精度将会从元组中读出
5.转换类型:http://www.cnblogs.com/nulige/p/6115793.html
from math import piprint('%10f' % pi)print('%10.2f' % pi)print('%010.2f' % pi)print('%-10.2f' % pi)x = 'abcde'print('%.2s' %x)
3.141593 3.140000003.143.14 ab
关于字符串对齐的方法:http://jingyan.baidu.com/article/77b8dc7fe1371b6174eab691.html
字符串方法
find
找到了就返回索引,找不到返回-1
#findx='deacddef'y = x.find('de')print(y)z = x.find('de',2,5) #设置起始点和结束点参数print(z)
0-1
join/split
两个互为逆方法
s = ['1','2','3']q ='+'print(q.join(s))m =(' ','usr','bin','env')print('C:'+'/'.join(m))print(q.join(s).split('+'))print(('C:'+'/'.join(m)).split('/'))
1+2+3C: /usr/bin/env['1', '2', '3']['C: ', 'usr', 'bin', 'env']
strip
返回去除两次(不包括内部)空格的字符串,也可以自定义参数去除两侧的其他字符
#stripx = ' 去除两侧空格 'print(x.strip())x= 5*'+'+x+5*'+'print(x)print((x.strip('+')).strip())
去除两侧空格+++++ 去除两侧空格 +++++去除两侧空格
translate
3.x的PYTHON的maketrans函数不需要再导入string模块,从使用string的maketrans函数变成,使用字符串的一种方法,用法如下
# translatex='123456'tab = x.maketrans('1235','abce') #(如果写成tab = x.maketrans('1235','abce','6')意思是在转换的同时把6删除掉)print(x.translate(tab))
参考:http://www.jb51.net/article/63987.htm
- PythonNewHere
- PythonNewHere
- PythonNewHere-模块导入
- PythonNewHere-Python开发环境搭建(windows)
- PythonNewHere--Python列表与元组基本函数与操作
- PythonNewHere -- 字符串与字典类型互相转换的方法
- win10服务主机占用cpu使用率该怎么办?win10服务主机cpu高的处理方法
- 剑指offer 46---实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字
- 谷歌官方MVP Demo解读
- POJ 1849 Two【树的直径+树的遍历】
- spring factory-bean用法
- PythonNewHere
- Vector用数组下标访问的条件
- 坦克类游戏的制作之路
- oracle按某两个字段的两个值排序
- 1、驱动应该怎么学
- JavaScript 判断两个数组是否相等 或者 判断一个数组中是否有重复的元素
- hibernate_级联操作、关联关系映射
- CodeFroces 831A. Unimodal Array(构造题)
- caffe.proto中的几个重要数据类型