首页 > 编程语言 > 详细

Python学习2——字符串

时间:2019-06-22 12:57:55      阅读:102      评论:0      收藏:0      [点我收藏+]

整数:int

二进制整数用0b作为前缀,只包含01两个字符。

十六进制整数用0x作为前缀,包含0-9a-f共6个字符。十六进制只能包含它规定的16个字符,其中a-f这6个字母不区分大小写。

浮点数:float

浮点数就是小数(如:1.222222,1.012344)。

布尔类型

布尔类型的值有两种:True,False.

布尔运算:or,and ,not

 

字符串

由字符组成。(如:‘hello’)

1.拼接字符串

   看看栗子:

>>> x = hello,

>>> y = world

>>> x+y          #直接像数字相加一样,相加

hello,world

 

2.字符串表示str和repr

>>> hello,world

hello,world      

#Python打印值时保留在代码中的样子,会用引号括起来

>>> print(hello,world)

hello,world                        #这个值才是我们希望用户看到的

 

加上换行符编码\n,再看看:

>>> x = hello,\nworld

>>> x

hello,\nworld

>>> print(x)

hello,

world

 

str是一个类,repr是一个函数:

>>> print(repr(hello,\nworld))        

hello,\nworld

>>> print(str(hello,\nworld))     #str将值转换为用户能够看懂的字符串

hello,

world

 

3.长字符串:跨越多行的字符串,需使用三引号(‘‘‘或者""")

>>> print(‘‘‘This is a very long string

Yes,very long

haha....‘‘‘)

This is a very long string

Yes,very long

haha....

 

常规字符串跨越多行,需要在行尾加上反斜杠\

>>> print(hello,
world)

hello,world

 

 

4.字符串格式设置

如果指定要将值插入在字符串中,可以用到%s(%s又叫转换说明符)。s意味着将值视为字符串进行格式设置。

>>> a  = hello,%s.%s enough for ya?

>>> v = (world,hot)

>>> a % v

hello,world.hot enough for ya?

 

在%左边指定一个字符串(带有格式),并在右边指定要设置其格式的值。值的设置,可以用单个值(字符串或数字),可以用元组(设置多个值)

>>> a = hello,%s!

>>> b = input(enter:)

enter:haha

>>> a % b

hello,haha!

>>> a = input(enter:)

enter:hh

>>> print(hello,%s%a)

 

%.3f将值的格式设置为包含3位小数的浮点数。

>>> c = This is %.3f!

>>> d = 2.2345

>>> c%d

This is 2.235!

 

使用关键字符参数:

>>> tmpl = Template(hello,$name!)

>>> tmpl.substitute(name=tt)

hello,tt!

 

使用花括号{}括起需要替换的字段:

>>> a = hello,{}

>>> a.format(cici)

hello,cici

将索引用作名称:

>>> {3} {0} {2} {1} {3} {0}.format(be,not,or,to)

to be or not to be

>>> {} {a} {} {bb}.format(1,2,a=5,bb=6)  #优先给未命名参数

1 5 2 6

>>> from math import pi

>>>{name} is approximately {value:.2f}..format(value=pi,name=π)

π is approximately 3.14.     #.2f指定要包含2位小数的浮点数格式

>>> fullname = [Alfred,Smoketoomuch]

>>> Mr {name[1]}.format(name=fullname)

Mr Smoketoomuch

 

5.join方法用于合并序列的元素,与split方法作用相反。

>>> s = [hello,,,world]

>>> d = ‘‘

>>> d.join(s)

hello,world

 

6.split方法将字符串拆分为序列。

>>> /user/bin/env.split(/)

[‘‘, user, bin, env]

 

7.translate方法可以同时替换一个字符串的多个字符

>>> table = str.maketrans(cs,kz)

>>> this is an incredible test.translate(table)

thiz iz an inkredible tezt

 

海龟绘图法:

模块turtle可以绘制非常有趣的图形。

  1. 绘制三角形

>>> from turtle import *     #导入turtle

>>> forward(100)

>>> left(120)

>>> forward(100)

>>> left(120)

>>> forward(100)

2.绘制五角星

>>> from turtle import *

>>> forward(100)

>>> right(144)

>>> forward(100)

>>> left(72)

>>> forward(100)

>>> right(144)

>>> forward(100)

>>> left(72)

>>> forward(100)

>>> right(144)

>>> forward(100)

>>> left(72)

>>> forward(100)

>>> right(144)

>>> forward(100)

>>> left(72)

>>> forward(100)

>>> right(144)

>>> forward(100)

动动手,感觉很有意思。

Python学习2——字符串

原文:https://www.cnblogs.com/suancaipaofan/p/11068430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!