当前位置:网站首页>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 .
边栏推荐
- vs+qt项目转qt creator
- C语言 踩坑:文档编码错误,导致base64中文编码错误
- 又到半年总结时,IT人只想躺平
- Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
- Newbeecoder. Page animation switching of UI control library
- mongodb项目中可能出现的坑
- c#数据库报错问题大家帮我看看吧
- RF content learning
- Long substring without repeating characters for leetcode topic resolution
- What is a PDCA cycle? How to integrate PDCA cycle and OKR
猜你喜欢

(1) Basic learning - Common shortcut commands of vim editor

Jour 13 Projet de santé mentale - chapitre 13

Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch

Day_ 11 smart communication health project - graphic report and poi Report

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

Ant Usage Summary (II): description of related commands

Machine learning 3-ridge regression, Lasso, variable selection technique

Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library

SSM project construction

How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
随机推荐
索引——MySQL
How to batch produce QR codes that can be read online after scanning
Pyqt5 setting window top left Icon
Day_ 03 smart communication health project - appointment management - inspection team management
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
[DaVinci developer topic] -41-app how SWC reads and writes NVM block data
什么是PDCA循环?如何整合 PDCA 循环和 OKR
WordPress aawp 3.16 cross site scripting
百度URL參數之LINK?URL參數加密解密研究(代碼實例)
Basic RF theory (DB)
JVM原理简介
Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits
Day_02 传智健康项目-预约管理-检查项管理
Find the number of nodes in the widest layer of a binary tree
同步开关电源降低EMI布局 dv/dt di/dt
【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
Progress of layer 2 technical scheme
Matplotlib savefig multiple picture overlay
RF content learning
CVE-2021-20038