设为首页 收藏本站
查看: 584|回复: 0

[经验分享] Python 入门教程 3 ---- Strings and Console Output

[复制链接]

尚未签到

发表于 2017-5-5 09:31:32 | 显示全部楼层 |阅读模式
  

  第一节
    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()
  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373265-1-1.html 上篇帖子: python编码错误小结以及保存方式修改 下篇帖子: 大数据系列3:用Python编写MapReduce
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表