当前位置:网站首页>Skilled use of slicing operations
Skilled use of slicing operations
2022-06-23 06:29:00 【Beauty of algorithm and programming】
0 introduction
stay python in , We often intercept the specified index range of a string , It is very complicated to operate with a loop structure , therefore python Provides slicing operations , One line of code can be used to experiment with operations that can only be completed by many loops , It greatly simplifies the process .
1 problem
Using slicing , Achieve one trim() function , Remove the space at the beginning and end of the string .
for example : Remove ‘ HelloWorld ’ First and last spaces .
2 Method
First step , The first character of a string can be clearly expressed as n=n[1:]; Second parts , The last character of the string can be clearly expressed as n=n[:-1]. Because I don't know the length of the string and the number of leading and trailing spaces , You can use two while loop , When n[0]==’ ( Space )’ when , Remove the leading and trailing spaces in turn , When n[len(n)-1:len(n)]==’ ( Space )’ when , Go out the space at the end , There is no space until the beginning and end , End of cycle .
3 Experimental results and discussion
Through the experiment 、 Practice has proved that the proposed method is effective , Is able to solve the problem raised at the beginning .
Code list 1
| def trim(n): while n[0]==' ': n=n[1:] while n[len(n)-1:len(n)]==' ': n=n[:-1] return n n=input(' Please enter a string :') print(trim(n)) |
4 Conclusion
For the problem of removing the closing space of the string , Propose slicing method , Through many experiments , The method is proved to be effective , The method in this paper makes use of slicing and loop operation to simplify the complex loop structure , It can provide good ideas for more complex loop operations in the future . At the same time, you can also review the definition of functions def sentence , Consolidate what you have learned .
边栏推荐
- Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control
- How to add libraries for Arduino ide installation
- Pyqt5 setting window top left Icon
- 【DaVinci Developer专题】-41-APP SWC如何读取写入NVM Block数据
- Vite learning (I) - Introduction
- [vivado] xilinxcedstore introduction
- Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)
- Day_09 传智健康项目-移动端开发-手机快速登录、权限控制
- Kotlin interface
- Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
猜你喜欢

Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)

Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits

Day_04 传智健康项目-预约管理-套餐管理

Efficient office of fintech (I): automatic generation of trust plan specification

【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)

原址 交换

Docker实战 -- 部署Redis集群与部署微服务项目

Layer 2技术方案进展情况

Day_ 12 smart health project jasperreports
随机推荐
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch
去除防火墙和虚拟机对live555启动IP地址的影响
[DaVinci developer topic] -42- how to generate template and header files of APP SWC
What is a PDCA cycle? How to integrate PDCA cycle and OKR
Cloud native database is the future
Wechat tried out the 1065 working system, and was forced to leave work at 18:00; It is said that Apple will no longer develop off screen fingerprint identification; Amd chief independent GPU architect
Tencent security 2021 report white paper collection (download attached)
Fastdata pole | insight report on e-commerce consumption of young Chinese users 2021
How to batch produce QR codes that can be read online after scanning
Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
Day_ 08 smart health project - mobile terminal development - physical examination appointment
基于T5L1的小型PLC设计方案
熟练利用切片操作
如何为 Arduino IDE 安装添加库
The central network and Information Technology Commission issued the National Informatization Plan for the 14th five year plan, and the network security market entered a period of rapid growth
Leetcode topic resolution single number
Day_ 05 smart communication health project - appointment management - appointment settings
Day_12 传智健康项目-JasperReports
Day_ 03 smart communication health project - appointment management - inspection team management
Memory analysis and memory leak detection