电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print

8页
  • 卖家[上传人]:海天
  • 文档编号:23993787
  • 上传时间:2017-10-22
  • 文档格式:PDF
  • 文档大小:220.97KB
  • / 8 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、StringsChapter 6Python for Informatics: Exploring IUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.htp:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles SeveranceString Data TypeA string is a sequence of charactersA string literal uses quotes Hello or “Hello”For strings, + means “concatenate”When a string contains numbers, it is stil a stringWe can convert numbers in a string into a number using int() str1 = Hel

      2、lo str2 = there bob = str1 + str2 print bobHellothere str3 = 123 str3 = str3 + 1Traceback (most recent cal last):File , line 1, in TypeError: cannot concatenate str and int objects x = int(str3) + 1 print x124 Reading and ConvertingWe prefer to read data in using strings and then parse and convert the data as we needThis gives us more control over error situations and/or bad user inputRaw input numbers must be converted from strings name = raw_input(Enter:)Enter:Chuck print nameChuck apple = raw

      3、_input(Enter:)Enter:100 x = apple - 10Traceback (most recent cal last):File , line 1, in TypeError: unsupported operand type(s) for -: str and int x = int(apple) - 10 print x90Loking Inside StringsWe can get at any single character in a string using an index specified in square bracketsThe index value must be an integer and starts at zeroThe index value can be an expression that is computed fruit = banana letter = fruit1 print lettera n = 3 w = fruitn - 1 print wn0b1a2n3a4n5aA Character To FarYo

      4、u wil get a python error if you attempt to index beyond the end of a string.So be careful when constructing index values and slices zot = abc print zot5Traceback (most recent cal last):File , line 1, in IndexError: string index out of range Strings Have LengthThere is a built-in function len that gives us the length of a string fruit = banana print len(fruit)60b1a2n3a4n5aLen Function fruit = banana x = len(fruit) print x6len()functionbanana (a string)6(a number)A function is some stored code tha

      5、t we use. A function takes some input and produces an output.Guido wrote this codeLen Functiondef len(inp):blahblahfor x in y:blahblahA function is some stored code that we use. A function takes some input and produces an output. fruit = banana x = len(fruit) print x6banana (a string)6(a number)Loping Through StringsUsing a while statement and an iteration variable, and the len function, we can construct a lop to lok at each of the letters in a string individualyfruit = bananaindex = 0while inde

      6、x s = Monty Python print s0:4Mont print s6:7P print s6:20Python0M1o2n3t4y56P7y8t9h10o11nIf we leave off the first number or the last number of the slice, it is asumed to be the beginning or end of the string respectivelySlicing Strings s = Monty Python print s:2Mo print s8:thon print s:Monty Python0M1o2n3t4y56P7y8t9h10o11nString ConcatenationWhen the + operator is applied to strings, it means concatenation a = Hello b = a + There print bHelloThere c = a + + There print cHello There Using in as a

      7、n OperatorThe in keyword can also be used to check to see if one string is in another stringThe in expression is a logical expression and returns True or False and can be used in an if statement fruit = banana n in fruitTrue m in fruitFalse nan in fruitTrue if a in fruit : . print Found it!. Found it! String Comparisonif word = banana:print All right, bananas.if word banana:print Your word, + word + , comes after banana.else:print All right, bananas.String LibraryPython has a number of string fu

      8、nctions which are in the string libraryThese functions are already built into every string - we invoke them by appending the function to the string variableThese functions do not modify the original string, instead they return a new string that has been altered greet = Hello Bob zap = greet.lower() print zaphello bob print greetHello Bob print Hi There.lower()hi therehttp:/docs.python.org/lib/string-methods.html stuff = Hello world type(stuff) dir(stuff)capitalize, center, count, decode, encode,

      9、 endswith, expandtabs, find, format, index, isalnum, isalpha, isdigit, islower, ispace, istitle, isupper, join, ljust, lower, lstrip, partition, replace, rfind, rindex, rjust, rpartition, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, translate, upper, zfillhttp:/docs.python.org/lib/string-methods.htmlstr.capitalize()str.center(width, fillchar)str.endswith(suffix, start, end)str.find(sub, start, end)str.lstrip(chars)str.replace(old, new, count)str.lower()str.rstrip(chars)str.strip(chars)str.upper()http:

      《Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print》由会员海天分享,可在线阅读,更多相关《Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2 2022年小学体育教师学期工作总结
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.