Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print
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
《Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print》由会员海天分享,可在线阅读,更多相关《Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print》请在金锄头文库上搜索。
红灰动态个性商务幻灯片模板简洁动态手势
蓝色清爽月度工作总结PPT模板简洁商务办公月度总结
动态蓝色拼图背景的工作计划PPT模板工作计划2
高架桥背景的工作计划PPT模板
Excel函数VBA教程基础入门(下)
唯美蓝色星空背景的动态工作总结PPT模板蓝色星空2
繁华城市背景的商业融资计划书PPT模板繁华城市
红灰动态个性商务幻灯片模板红灰个性商务
Py4Inf-06-Strings-PrintPy4Inf-06-Strings-Print
Py4Inf-05-IterationsPy4Inf-05-Iterations
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print
Py4Inf-04-FunctionsPy4Inf-04-Functions
Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print
Py4Inf-03-ConditionalPy4Inf-03-Conditional
Py4Inf-03-Conditional-PrintPy4Inf-03-Conditional-Print
Py4Inf-02-ExpressionsPy4Inf-02-Expressions
Py4Inf-02-Expressions-PrintPy4Inf-02-Expressions-Print
Py4Inf-01-IntroPy4Inf-01-IntroPy4Inf-01-IntroPy4Inf-01-Intro
Py4Inf-01-Intro-PrintPy4Inf-01-Intro-Print
JAVA初级入门教程
2024-03-05 77页
2024-03-05 50页
2024-02-29 54页
2024-02-29 75页
2024-02-29 52页
2024-02-29 54页
2024-02-29 125页
2024-02-29 74页
2024-02-29 61页
2024-02-29 56页