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

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
主站蜘蛛池模板: 龙南县| 孝感市| 长海县| 阳东县| 邵武市| 大丰市| 达日县| 万州区| 庐江县| 临海市| 且末县| 阿尔山市| 洛浦县| 庆云县| 娱乐| 长葛市| 沁水县| 原阳县| 临夏市| 鄂尔多斯市| 巴里| 碌曲县| 东丽区| 胶州市| 钦州市| 洱源县| 苗栗县| 舒兰市| 新郑市| 徐水县| 永康市| 福建省| 庆云县| 晋中市| 孝昌县| 乐平市| 罗源县| 密云县| 山东| 祁连县| 河源市|