- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 155字
- 2021-07-02 14:00:20
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
推薦閱讀
- 數據結構與算法JavaScript描述
- Scratch 3.0少兒編程與邏輯思維訓練
- Troubleshooting PostgreSQL
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- C語言程序設計上機指導與習題解答(第2版)
- 計算機應用基礎實踐教程
- Java網絡編程實戰
- Mastering Git
- Visual Studio 2015高級編程(第6版)
- Developing SSRS Reports for Dynamics AX
- Orleans:構建高性能分布式Actor服務
- 算法圖解
- Training Systems Using Python Statistical Modeling
- Python預測之美:數據分析與算法實戰(雙色)
- Xamarin Cross-Platform Development Cookbook