Python模擬刪除字符串兩邊的空白

目標:
  1.使用string模塊的whitespace
  2.刪除左邊、右邊以及兩邊的空白

代碼如下:

[root@localhost python]# cat rmspace.py

#!/usr/bin/env python
#coding:utf8
"""
使用字符串刪除左右兩端的空白。
"""from string import whitespace#刪除左邊的空白
def lrmsps(astr):for i in xrange(len(astr)):if astr[i] not in whitespace:return astr[i:]#當輸入的全是空白字符時,返回空return ''#刪除右邊的空白,從列表的右邊開始判斷
def rrmsps(astr):for i in reversed(xrange(len(astr))):if astr[i] not in whitespace:return astr[:(i+1)]return ''#刪除左右兩邊的空白
def rmsps(astr):return rrmsps(lrmsps(astr))if __name__ == '__main__':hi = '    hello,world.      'print '刪除左邊空白:|%s|' % lrmsps(hi)print '刪除右邊空白:|%s|' % rrmsps(hi)print '刪除兩邊空白:|%s|' % rmsps(hi)

?

2.運行代碼,測試效果

[root@localhost python]# python rmspace.py
刪除左邊空白:|hello,world.      |
刪除右邊空白:|    hello,world.|
刪除兩邊空白:|hello,world.|

?

?

*附錄:使用list的方式模擬刪除字符串左右兩邊的空白

代碼如下:

#!/usr/bin/env python
#coding:utf8
"""
使用列表的方式刪除左右兩端的空白。
"""
from string import whitespacedef lrmsps(astr):result = list(astr)for i in xrange(len(result)):if result[0] not in whitespace:breakresult.pop(0)return ''.join(result)def rrmsps(astr):result = list(astr)for i in xrange(len(result)):if result[-1] not in whitespace:breakresult.pop()return ''.join(result)def rmsps(astr):return rrmsps(lrmsps(astr))if __name__ == '__main__':hi = '     hello,world.    'print '|%s|' % lrmsps(hi)print '|%s|' % rrmsps(hi)print '|%s|' % rmsps(hi)

?

轉載于:https://www.cnblogs.com/xkops/p/6244198.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541983.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541983.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541983.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

xml分析錯誤:注釋未終止_錯誤:C中的未終止注釋(無效的注釋塊) 常見的C程序錯誤...

xml分析錯誤:注釋未終止Comments are used to write logic explain or anything that you do not want to compile. In C language there are two types of comments 1) Single line comment and 2) Multi-line comment. 注釋用于編寫邏輯解釋或您不想編譯的任何內容。 在C語言…

查看 mysql 狀態_查看mysql狀態的常用命令

在mysql客戶端輸入"show status"之后將會看到如下輸出:如果想要查看某個具體的值,可以使用如下命令:show status LIKE "%具體變量%";Aborted_clients 由于客戶沒有正確關閉連接已經死掉,已經放棄的連接數量.A…

常用數學符號的讀法及其含義

2019獨角獸企業重金招聘Python工程師標準>>> 常用數學符號的讀法及其含義 近來發現很多學生對一些數學符號的讀法及其含義不是很清楚。今天特把一些常用的列表如下。希望能夠提供一些幫助! 大寫 小寫 英文注音 國際音標注音 中文注音 Α…

math.atan_JavaScript中帶有示例的Math.atan()方法

math.atanJavaScript | Math.atan()方法 (JavaScript | Math.atan() Method) Math.atan() is a function in math library of JavaScript that is used to find the value of arctangent of a number. Math.atan()是JavaScript數學庫中的函數,用于查找數字的反正切值…

mysql 計算工作日_mysql計算工作日_MySQL

bitsCN.commysql計算工作日Sql代碼 DELIMITER $$ drop procedure if exists pGetWorkDays$$ create procedure pGetWorkDays(s datetime,e datetime) begin select floor(days/7)*5days%7 -case when 6 between wd and wddays%7-1 then 1 else 0 end -case when 7 between wd a…

后端碼農談前端(HTML篇)第三課:常見屬性

一、HTML全局屬性 1、核心屬性 屬性描述id設置元素的唯一 id。class設置元素的一個或多個類名(引用樣式表中的類)。style設置元素的行內樣式(CSS內聯樣式)。title設置有關元素的額外信息(可在工具提示中顯示&#xff0…

mysql數據庫的服務無法啟動_mysql5數據庫服務無法啟動

mysql5數據庫服務無法啟動我最近安裝了MySQL 5.0 Community Edition因為聽很多人介紹,mysql開源,免費,速度快,于是終于按捺不住,嘗試一下。誰知這一嘗試,噩夢就來了。[安裝環境]windows xp sp2MySQL 5.0 Co…

math.asin_JavaScript中帶有示例的Math.asin()方法

math.asinJavaScript | Math.asin()方法 (JavaScript | Math.asin() Method) Math.asin() is a function in math library of JavaScript that is used to find the value of arcsine of a number and return the value in radians. Math.asin()是JavaScript數學庫中的函數&…

POJ 3422 費用流

思路&#xff1a; 把每個方塊拆成兩個點 1個入點 1個出點 當前格子的入->出連費用-w[i][j] 容量1的邊 當前格子的入->出連費用0 容量k-1的邊 此格子的出向右&下&#xff08;如果有的話&#xff09;的格子的入連費用0容量k的邊 //By SiriusRen #include <queu…

mysql內置變量_MySQL常用內置變量

MySQL用很多常用的內置變量&#xff0c;掌握這些內置變量后對于我們快速獲取當前MySQL的配置有很大幫助&#xff0c;下面就來列舉幾個常用的變量。查看當前MySQL版本號信息。show variables like version;MariaDB [(none)]> show variables like version;------------------…

Android-TextView跑馬燈效果

要實現跑馬燈還是比較簡單的。 同時有幾個需要注意的點&#xff0c;先上代碼&#xff1a; 1 public class MTView extends TextView {2 3 4 public MTView(Context context) {5 super(context);6 }7 8 public MTView(Context context, AttributeSet attrs)…

qt沒有mysql文件夾_qt5-qt目錄下沒有mysql文件夾

我安裝的qt5.2.1&#xff0c;為什么我的qt安裝目錄下沒有 (例如&#xff1a;D:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\)mysql這個文件夾&#xff1f;無法Create MySQL driver for Qt5 on WindowsWindows下解決QSqlDatabase:QMySQL driver not loaded解決參考…

Linux Shell命令能力傾向問題和解答

This section contains Aptitude Questions and Answers on Linux Shell Commands. 本節包含有關Linux Shell命令的 Aptitude問答。 1) Which of the following command is used to check a Linux command is a built-in shell command or external command? cmdtypetypectyp…

php mysql 權重_PHP對MySql的常用操作

關于PHP對MySql的常用操作最近做網站&#xff0c;用PHP操作數據庫也很多次了&#xff0c;但總是忘記&#xff0c;參考了網上的很多資料&#xff0c;算是整理記錄下。數據庫操作類實現數據庫的連接&#xff0c;斷開&#xff0c;以及請求&#xff1a;/*** Created by PhpStorm.* …

Python timedelta total_seconds()方法與示例

Python timedelta.total_seconds()方法 (Python timedelta.total_seconds() Method) timedelta.timedeltotal_seconds() method is used in the timedelta class of module datetime. timedelta.timedeltotal_seconds()方法在模塊datetime的timedelta類中使用。 It uses an i…

static和extern對函數的作用

2019獨角獸企業重金招聘Python工程師標準>>> 外部函數&#xff1a;定義的函數能被本文件和其他文件訪問 默認情況下所有函數都是外部函數 不允許有同名的外部函數內部函數&#xff1a;定義的函數只能被本文件訪問&#xff0c;其他文件不能訪問 允許不同文件中有同名…

MySQL從服務器寫入報錯嗎_MySQL主從復制讀寫分離及奇怪的問題

一直都沒有寫blog的習慣&#xff0c;以前總覺得自己的腦子就是最好的記憶容器&#xff0c;現在覺得我好像有個假腦子。當時是使用阿里云鏡像&#xff0c;安裝了兩臺ECS&#xff0c;結果配置MySQL的時候出現了UUID重復問題。先從配置主從開始吧&#xff0c;值得記錄。文中很多部…

python二分法查找程序_查找Python程序的輸出| 套裝2(基礎)

python二分法查找程序Program 1: 程序1&#xff1a; a 10b 3res a/b print "a/b: ", res res float(a/b)print "float (a/b) : ", res res float (a) /float (b)print "float (a/b) : ", res res a/b print "a/b: ", resOutput…

數據庫 數據庫SQL語句一

字符和日期 --字符和日期都要包含在單引號中 --字符大小寫敏感&#xff0c;日期格式敏感 --默認的日期格式是DD-MON-RR--查詢當前系統時間 SQL> select sysdate from dual; --查詢工資在1000~2000之間的員工信息 SQL> select * from emp where sal>1000 and sal<20…

mysql text保存圖片_用mysql 如果包含有文字和圖片,那么我要用哪種數據類型存儲呢?還是分開,用TEXT和BLOB嗎?...

rootytt:/var/lib/mysql-files# for i in seq 1 100; do cp 微信圖片_20190711095019.jpg "$i".jpg;done;rootytt:/var/lib/mysql-files# ls100.jpg 17.jpg 25.jpg 33.jpg 41.jpg 4.jpg 58.jpg 66.jpg 74.jpg 82.jpg 90.jpg 99.jpg f8.tsv10.jpg 18…