Python学习笔记(六)- 字符串基本原理(String Fundamentals)

1.字符串find方法可以被用来搜索列表么?
答:不可以,方法(methods)总是特定类型的;那就是说,它们仅在单种数据类型上起作用。表达式像是 X+Y 和内建函数像是 len(X) 则是通用的,可以在多种数据类型上起作用。在这种情况下,例如,in 成员表达式和字符串find方法有相似的效果,但是它可以被用来搜索字符串和列表。在Python 3.X 中,有一些人尝试按类别对方法(methods)进行分组(例如,可变的序列类型list和bytearray有相似的方法集),但是方法比其它操作集更加具有特定类型的特征。)

 

2.字符串切片表达式可以被用在列表上么?
答:可以,不像方法,表达式是通用的而且可以应用到许多类型。在这种情况下,切片表达式事实上是序列(sequence)操作——任何序列对象的种类它都起作用,包括字符串(strings),列表(lists)和元组(tuples)。仅有的区别是,当你对列表切片是,返回的是一个新的列表。

 

3.如何把字符转为它的ASCII码?那,ASCII码转为字符?
答:内建函数 ord(S) 把单个字符转化为整型状态的字符代码;char(I) 把整型字符代码转化为字符串。但是,记住,这些整数仅能是只从ASCII字符集提取字符的文本的ASCII代码。在unicode模型中,文本字符串实际上是unicode代码点标识整数的序列,可能不属于ASCII保留的7位数字范围。

 

4.在Python中,如何改变字符串?
答:字符串不可以被改变;它们是不可变的(Immutable)。然而,你可用创建一个新的字符串来取得相似的效果,或者用方法(method)调用像是 replace ——然后把结果赋值回原来的变量名字。

 

5.当S="s,pa,m",说出两种方法提取中间的两个字符。
答:可以用 S[2:4] 切片字符串,或者用逗号 split 然后用 S.split(',')[1]索引字符串。试着用交互模式来看一看。

 

6.在字符串"a\nb\x1f\000d"中有几个字符?
答:6个。字符串"a\nb\x1f\000d"包含:字符a,换行符(\n),字符b,二进制31(16进制转义\x1f),二进制0(8进制转义\000),和字符d。把字符串传入内建函数len中可以证实这一结果,print每一个字符的ord结果来查看真实代码点(标识号)值。

 

7.为什么可以使用字符串模块而不是字符串方法调用?
答:目前,你应该永远不要使用string模块来取代字符串对象方法——它被反对,而且在Python 3.X中被完全移除了。目前,使用字符串模块的唯一有效原因是它的其他工具,如预定义常量。您可能还会看到它出现在现在已经非常古老、尘土飞扬的Python代码中(以及关于迷雾重重过去的书籍-比如上世纪90年代的书籍)。

 

标注:转载《Learning Python 5th Edition》[奥莱理]

1. Can the string find method be used to search a list?
2. Can a string slice expression be used on a list?
3. How would you convert a character to its ASCII integer code? How would you
convert the other way, from an integer to a character?
4. How might you go about changing a string in Python?
5. Given a string S with the value "s,pa,m", name two ways to extract the two characters in the middle.
6. How many characters are there in the string "a\nb\x1f\000d"?
7. Why might you use the string module instead of string method calls?

1. No, because methods are always type-specific; that is, they only work on a single data type. Expressions like X+Y and built-in functions like len(X) are generic, though, and may work on a variety of types. In this case, for instance, the in membership expression has a similar effect as the string find, but it can be used to search both strings and lists. In Python 3.X, there is some attempt to group methods by categories (for example, the mutable sequence types list and bytearray have similar method sets), but methods are still more type-specific than other operation sets.
2. Yes. Unlike methods, expressions are generic and apply to many types. In this case, the slice expression is really a sequence operation—it works on any type of sequence object, including strings, lists, and tuples. The only difference is that when you slice a list, you get back a new list.
3. The built-in ord(S) function converts from a one-character string to an integer character code; chr(I) converts from the integer code back to a string. Keep in mind, though, that these integers are only ASCII codes for text whose characters are drawn only from ASCII character set. In the Unicode model, text strings are really sequences of Unicode code point identifying integers, which may fall outside the 7-bit range of numbers reserved by ASCII (more on Unicode in Chapter 4 and Chapter 37).
4. Strings cannot be changed; they are immutable. However, you can achieve a similar effect by creating a new string—by concatenating, slicing, running formatting expressions, or using a method call like replace—and then assigning the result back to the original variable name.
5. You can slice the string using S[2:4], or split on the comma and index the string using S.split(',')[1]. Try these interactively to see for yourself.
6. Six. The string "a\nb\x1f\000d" contains the characters a, newline (\n), b, binary 31 (a hex escape \x1f), binary 0 (an octal escape \000), and d. Pass the string to the built-in len function to verify this, and print each of its character’s ord results to see the actual code point (identifying number) values. See Table 7-2 for more details on escapes.
7. You should never use the string module instead of string object method calls today —it's deprecated, and its calls are removed completely in Python 3.X. The only valid reason for using the string module at all today is for its other tools, such as predefined constants. You might also see it appear in what is now very old and dusty Python code (and books of the misty past—like the 1990s).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值