集团站切换校区

验证码已发送,请查收短信

复制成功
微信号:togogoi
添加微信好友, 详细了解课程
已复制成功,如果自动跳转微信失败,请前往微信添加好友
打开微信
图标

学习文章

当前位置:首页 > >学习文章 > >

{人工智能}python編程之字符串

发布时间: 2018-08-03 22:34:20

​实验介绍

关于本实验

本实验主要介绍了 Python 字符串的相关知识点和简单操作。

实验目的

l 理解 Python 字符串的含义。

l 掌握 Python 字符串相关的操作。

 实验任务配置​

  概念知识​

1.Python 字符串。Python 中的字符串是零个或多个的字符所组成的序列,字符串是 Python 内建的 6 种序列之一,在 Python 中字符串是不可变的,即我们在 C、C++语言中常说的字符串常量。

2.字符串表示方法。Python 的字符串表示有单引号、双引号和三引号,还有转义字符、原始字符串等。

实验内容​

步骤 1 单引号、双引号

单引号、双引号是一样的,它们可以相互转换。

>>> s = 'python string'

>>> print(s) python string

>>> ss="python string"

>>> print(ss) python string

>>> sss='python "Hello World"string'

>>> print(sss)

python "Hello World"string

步骤 2 长字符串






前面提到了三引号''',在 python 中三引号可以定义长字符串传。长字符串的输出,如:

>>> print('''this is a long string''') this is a long string

步骤 3 原始字符串

原始字符串以 r 开头,可以在原始字符串中放入任何字符,最后输出的字符串包含了转义所用的反斜线,但是不能在字符串结尾输入反斜线。如:

>>> rawStr = r'D:\SVN_CODE\V900R17C00_TRP\omu\src'

>>> print(rawStr)

D:\SVN_CODE\V900R17C00_TRP\omu\src

步骤 4 字符串宽度、精度及对齐

要想实现字符串宽度、精度及对齐的效果,就需要参考格式化操作符辅助指令了。

>>> print("%c" % 97) a

>>> print("%6.3f" % 2.5) 2.500

>>> print("%+10x" % 10)

+a

>>> print("%.*f" % (4, 1.5))

1.5000

步骤 5 连接、重复

Python 中“+、*”可以做字符串连接操作。

>>> s = 'I' + 'want' + 'Python' + '.'

>>> print(s) IwantPython.

>>> ss='Python'*3

>>> print(ss)

PythonPythonPython

步骤 6 删除字符串

del 用于删除字符串对象。删除后该对象不再存在,再次访问该对象会报错。

>>> ss = 'Python'*3

>>> print(ss) PythonPythonPython

>>> del ss

>>> print(ss)

Traceback (most recent call last):

File "<pyshell#35>", line 1, in <module> print(ss NameError: name 'ss' is not defined)







上一篇: {人工智能}python編程之函数

下一篇: {人工智能}python编程之字典

十五年老品牌
微信咨询:togogoi 咨询电话:18922156670 咨询网站客服:在线客服

相关课程推荐

在线咨询 ×

您好,请问有什么可以帮您?我们将竭诚提供最优质服务!