Python 爬虫基础知识

1、Python中字符串类型的相关梳理

    1.1 正常来说Python使用换行作为语句终结,但是在圆括号、方括号、花括号或者三引号内的字符串是例外。如果写一个长字符串,跨越2行或者多行,但是不用三引号字符串,有两种解决办法

   (1) t = " This is not the best way to join two long strings” +\

      “together since it relies on ugly newline escaping”

    (2)  s  =( “This the nice way to join two long string”

“together; it relies on string literal concatenation”)

第(2)种情况使用圆括号将其包含在一起。Python的“Idioms and Anti-Idioms” Howto 文档建议总是使用圆括号将跨越多行的任何语句进行封装,而不使用转义的换行符。

   1.2 原始字符串用r引导

   1.3 str.format()进行字符串格式化

   “The novel {0} was published in {1} ”.format(“Hard Times”,1854)如果需要在格式化字符串中包含花括号,将就需要将其复写“{{{0}}}.format(“I’am braces”)”

   1.4 3种字符类型 str用于Unicode文本,bytes用于二进制数据,bytearray是bytes的一种可变的变体

   1.5 Python自动在任意的表达式中合并相邻的字符串常量

title = “Meaning ” ‘of’ “ Life”

posted @ 2018-10-25 16:59  hola荞麦  阅读(189)  评论(0编辑  收藏  举报