官术网_书友最值得收藏!

Strings

Like numbers, strings are also one of the data structures in Python. Python can manipulate strings. Strings can be expressed as follows:

  • Enclosed in single quotes ('...')
  • Enclosed in double quotes ("...")

See the following example:

>>> 'Hello Python'
'Hello Python'
>>> "Hello Python"
'Hello Python'

A string is a set of characters. We can access the characters one at a time, as follows:

>>> city = 'delhi'
>>> letter = city[1]
>>> letter = city[-3]

In the second statement, we are selecting the character number 1 from city and assigning it to letter. The number in those square brackets is an index. The index indicates which character you want to access. It starts from 0. So, in the preceding example, when you will execute letter = city[1], you will get the following output:

city d e l h i
index 0 1 2 3 4
-5 -4 -3 -2 -1

Output:
e
l
主站蜘蛛池模板: 南汇区| 商洛市| 乌兰县| 称多县| 沙湾县| 龙泉市| 扶绥县| 深州市| 临洮县| 苏尼特右旗| 安新县| 栖霞市| 兰坪| 随州市| 高清| 镇坪县| 安化县| 柘城县| 新宁县| 白河县| 曲松县| 洛南县| 施甸县| 临江市| 鄱阳县| 郓城县| 库尔勒市| 安庆市| 马山县| 西宁市| 赤水市| 镇沅| 龙门县| 高淳县| 永安市| 南投市| 小金县| 佳木斯市| 津南区| 麻城市| 迭部县|