字符串类型 字符串:字符序列 一字符、字符集(计算机认识的符号) 字符串字面值:用一对引号(单或双)标明 “hello world" ‘NI@#$%A&*' “汉字也是字符” 注意:尽量不要用汉字; 引号的配对,良好的风格是同时打出来 3
3
字符串类型 ■ 字符串中包含引号怎么办? 一包含单引号:字符串用双引号括住 -包含双引号:字符串用单引号括住 两者都有:更一般的方法是串中用escape字符 print "Alice said,\"Hello,'Bob'm 将输出:A1 ice said,"Hel1o,/Bob'" 4
4
字符串的输入 错误输入: >>>str input("Enter a string:" Enter a string:John 原因:input()是把输入当成表达式来 计算的! 解决方法: -输入时加上引号 使用raw input() 6
6
input.与raw_input 例:比较 >>>x input() >>>x raw input() "John" John >>>x >>>x >>>x input() >>>x= raw input() 3*4+2 3*4+2 >>>x >>>x 可见raw input()将输入当成字符串 数据 7
7