- Hands-On Penetration Testing with Python
- Furqan Khan
- 239字
- 2021-07-02 14:13:48
Substrings or string slicing
Obtaining part of the string is a common exercise that we come across frequently in day-to-day string operations. Languages such as C or Java provide us with dedicated methods such as substr(st_index,end_index) or subString(st_index,end_index). To perform the substring operation in Python, there is no dedicated method, but we can instead use slicing. For example, if we wish to get the first four characters of our original my_str string, we can achieve this by using operations such as my_str[0:4], as shown in the following screenshot:

Again, the slice operation returns a new string and the changes are not applied to the original string. Furthermore, it is worth understanding here that the slicing happens over n-1 characters, where n is the upper limit, specified as the second parameter, which is four, in our case. Thus, the actual substring operation will be performed starting from index 0 and ending at index 3, thus returning the string Welc.
Let's take a look at some more examples of slicing:
To get the whole string from index 4, do the following:

To get the string from the start up to index 4, do the following:

To print the whole string with slicing, do the following:

To print the characters with a step of 2, do the following:

To print the reverse of the string, do the following:

To print a part of the string in reverse order, to the following:

- JavaScript從入門到精通(微視頻精編版)
- 案例式C語言程序設(shè)計
- Mastering Articulate Storyline
- 機器人Python青少年編程開發(fā)實例
- 基于免疫進化的算法及應(yīng)用研究
- C/C++常用算法手冊(第3版)
- MySQL數(shù)據(jù)庫基礎(chǔ)實例教程(微課版)
- Android嵌入式系統(tǒng)程序開發(fā):基于Cortex-A8(第2版)
- C++編程兵書
- Modern C++ Programming Cookbook
- Learning Nessus for Penetration Testing
- Clojure High Performance Programming(Second Edition)
- Learning Concurrency in Python
- 人人都能開發(fā)RPA機器人:UiPath從入門到實戰(zhàn)
- Xamarin Cross-Platform Development Cookbook