Python 入门教程 3 ---- Strings and Console Output

本文详细介绍Python中字符串的各种操作方法,包括定义、转义字符、获取长度、大小写转换、数值转换及格式化等,并提供了丰富的实例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


第一节

1 Python里面还有一种好的数据类型是String

2 一个String是通过'' 或者 ""包成的串

3 设置变量brian值为"Always look on the bright side of life!"

#Set the variable brian on line 3!
brian = "Always look on the bright side of life!"

第二节

1 练习

1 把变量caesar变量设置为Graham

2把变量praline变量设置为john

3把变量viking变量设置为Teresa

    #Assign your variables below, each on its own line!
    caesar = "Graham"
    praline = "John"
    viking = "Teresa"
    #Put your variables above this line
    print caesar
    print praline
    print viking

第三节

1 Python是通过\来实现转义字符的

2 练习把'Help! Help! I'm being repressed!' 中的I'm中的'进行转义

   #The string below is broken. Fix it using the escape backslash!
   'Help! Help! \'\m being repressed!'

第四节

1 我们可以使用""来避免转义字符的出现

2 练习: 把变量fifth_letter设置为MONTY的第五个字符

   """
   The string "PYTHON" has six characters,
   numbered 0 to 5, as shown below:

   +---+---+---+---+---+---+
   | P | Y | T | H | O | N |
   +---+---+---+---+---+---+
     0   1   2   3   4   5

   So if you wanted "Y", you could just type
   "PYTHON"[1] (always start counting from 0!)
   """
   fifth_letter = "MONTY"[4]

   print fifth_letter


第五节

1 介绍String的第一种方法,len()求字符串的长度

2 练习: 把变量parrot的值设置为"Norweigian Blue",然后打印parrot的长度

   parrot = "Norwegian Blue"
   print len(parrot)


第六节

1 介绍String的第二种方法,lower()把所有的大写字母转化为小写字母

2 练习: 把parrot中的大写字母转换为小写字母并打印

   parrot = "Norwegian Blue"
   print parrot.lower()


第七节

1 介绍String的第三种方法,upper()把所有的大写字母转化为小写字母

2 练习: 把parrot中的小写字母转换为大写字母并打印

   parrot = "norwegian blue"
   print parrot.upper()

第八节

1 介绍String的第四种方法,str()把非字符串转化为字符串,比如str(2)是把2转化为字符串"2"

2 练习: 设置一个变量pi值为3.14 , 把pi转化为字符串

   """Declare and assign your variable on line 4,
   then call your method on line 5!"""

   pi = 3.14
   print str(pi)


第九节

1 主要介绍“.” 的用处,比如上面的四个String的四个方法都是用到了点

2 练习: 利用“.”来使用String的变量ministry的函数len()和upper(),并打印出

ministry = "The Ministry of Silly Walks"

print len(ministry)
print ministry.upper()

第十节

1 介绍print的作用

2 练习:利用print输出字符串"Monty Python"

"""Tell Python to print "Monty Python"
to the console on line 4!"""

print "Monty Python"

第十一节

1 介绍print来打印出一个变量

2 练习:把变量the_machine_goes值赋值"Ping!",然后打印出

"""Assign the string "Ping!" to
the variable the_machine_goes on
line 5, then print it out on line 6!"""

the_machine_goes = "Ping!"
print the_machine_goes

第十二节

1 介绍我们可以使用+来连接两个String

2 练习:利用+把三个字符串"Spam "和"and "和"eggs"连接起来输出

# Print the concatenation of "Spam and eggs" on line 3!

print "Spam " + "and " + "eggs"

第十三节

1 介绍了str()的作用是把一个数字转化为字符串

2 练习:利用str()函数把3.14转化为字符串并输出

# Turn 3.14 into a string on line 3!

print "The value of pi is around " + str(3.14)


第十四节

1 介绍了字符串的格式化,使用%来格式化,字符串是%s

2 举例:有两个字符串,利用格式化%s来输出

string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

第十五节

1 回顾之前的内容

2 练习

1 设置变量my_string的值

2 打印出变量的长度

3 利用upper()函数并且打印变量值

# Write your code below, starting on line 3!

my_string = "chenguolin"
print len(my_string)
print my_string.upper()



PS D:\science research\deep learning\core codes and datas\rl_learning> pip list Package Version ------------------------- -------------- anyio 4.6.2.post1 argon2-cffi 23.1.0 argon2-cffi-bindings 21.2.0 arrow 1.3.0 asttokens 2.4.1 async-lru 2.0.4 attrs 24.2.0 babel 2.16.0 beautifulsoup4 4.12.3 bleach 6.2.0 certifi 2024.8.30 cffi 1.17.1 charset-normalizer 3.4.0 colorama 0.4.6 comm 0.2.2 contourpy 1.3.1 cycler 0.12.1 debugpy 1.8.9 decorator 5.1.1 defusedxml 0.7.1 executing 2.1.0 fastjsonschema 2.20.0 fonttools 4.55.3 fqdn 1.5.1 h11 0.14.0 httpcore 1.0.7 httpx 0.27.2 idna 3.10 imageio 2.36.1 imageio-ffmpeg 0.5.1 ipykernel 6.29.5 ipython 8.29.0 ipywidgets 8.1.5 isoduration 20.11.0 jedi 0.19.2 Jinja2 3.1.4 json5 0.9.28 jsonpointer 3.0.0 jsonschema 4.23.0 jsonschema-specifications 2024.10.1 jupyter 1.1.1 jupyter_client 8.6.3 jupyter-console 6.6.3 jupyter_core 5.7.2 jupyter-events 0.10.0 jupyter-lsp 2.2.5 jupyter_server 2.14.2 jupyter_server_terminals 0.5.3 jupyterlab 4.2.6 jupyterlab_pygments 0.3.0 jupyterlab_server 2.27.3 jupyterlab_widgets 3.0.13 kiwisolver 1.4.7 MarkupSafe 3.0.2 matplotlib 3.10.0 matplotlib-inline 0.1.7 mistune 3.0.2 nbclient 0.10.0 nbconvert 7.16.4 nbformat 5.10.4 nest-asyncio 1.6.0 notebook 7.2.2 notebook_shim 0.2.4 numpy 2.1.3 overrides 7.7.0 packaging 24.2 pandas 2.2.3 pandocfilters 1.5.1 parso 0.8.4 pillow 11.0.0 pip 24.3.1 platformdirs 4.3.6 prometheus_client 0.21.0 prompt_toolkit 3.0.48 psutil 6.1.0 pure_eval 0.2.3 pycparser 2.22 Pygments 2.18.0 pyparsing 3.2.0 python-dateutil 2.9.0.post0 python-json-logger 2.0.7 pytz 2024.2 pywin32 308 pywinpty 2.0.14 PyYAML 6.0.2 pyzmq 26.2.0 referencing 0.35.1 requests 2.32.3 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 rpds-py 0.21.0 Send2Trash 1.8.3 setuptools 75.6.0 six 1.16.0 sniffio 1.3.1 soupsieve 2.6 terminado 0.18.1 tinycss2 1.4.0 tornado 6.4.2 traitlets 5.14.3 types-python-dateutil 2.9.0.20241003 tzdata 2024.2 uri-template 1.3.0 urllib3 2.2.3 wcwidth 0.2.13 webcolors 24.11.1 webencodings 0.5.1 websocket-client 1.8.0 widgetsnbextension 4.0.13 PS D:\science research\deep learning\core codes and datas\rl_learning> pip install gym==0.18.3 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting gym==0.18.3 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/1a/db/816fd52c0c196b6799e89d1f65b6c74fead2707cf7d447f3f354edfa7a44/gym-0.18.3.tar.gz (1.6 MB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [3 lines of output] C:\Users\Zhang\AppData\Local\Temp\pip-build-env-ugk09oli\overlay\Lib\site-packages\setuptools\_distutils\dist.py:289: UserWarning: Unknown distribution option: 'tests_require' warnings.warn(msg) error in gym setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. [notice] A new release of pip is available: 24.3.1 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值