python單詞首字母大寫_在Python中將每個單詞的首字母大寫

python單詞首字母大寫

Here, we are implementing a python program to capitalizes the first letter of each word in a string.

在這里,我們正在實現一個python程序來大寫字符串中每個單詞的首字母。

Example:

例:

    Input: "HELLO WORLD!"
Output: "Hello World!"

Method 1: Using title() method

方法1:使用title()方法

# python program to capitalizes the 
# first letter of each word in a string
# function 
def capitalize(text):
return text.title()
# main code 
str1 = "Hello world!"
str2 = "hello world!"
str3 = "HELLO WORLD!"
str4 = "includehelp.com is a tutorials site"
# printing
print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)
print("str4: ", str4)
print()
print("capitalize(str1): ", capitalize(str1))
print("capitalize(str2): ", capitalize(str2))
print("capitalize(str3): ", capitalize(str3))
print("capitalize(str4): ", capitalize(str4))

Output

輸出量

str1:  Hello world!
str2:  hello world!
str3:  HELLO WORLD!
str4:  includehelp.com is a tutorials site
capitalize(str1):  Hello World!
capitalize(str2):  Hello World!
capitalize(str3):  Hello World!
capitalize(str4):  Includehelp.Com Is A Tutorials Site

Method 2: Using loop, split() method

方法2:使用循環,split()方法

# python program to capitalizes the 
# first letter of each word in a string
# function 
def capitalize(text):
return  ' '.join(word[0].upper() + word[1:] for word in text.split())
# main code 
str1 = "Hello world!"
str2 = "hello world!"
str3 = "HELLO WORLD!"
str4 = "includehelp.com is a tutorials site"
# printing
print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)
print("str4: ", str4)
print()
print("capitalize(str1): ", capitalize(str1))
print("capitalize(str2): ", capitalize(str2))
print("capitalize(str3): ", capitalize(str3))
print("capitalize(str4): ", capitalize(str4))

Output

輸出量

str1:  Hello world!
str2:  hello world!
str3:  HELLO WORLD!
str4:  includehelp.com is a tutorials site
capitalize(str1):  Hello World!
capitalize(str2):  Hello World!
capitalize(str3):  HELLO WORLD!
capitalize(str4):  Includehelp.com Is A Tutorials Site

翻譯自: https://www.includehelp.com/python/capitalizes-the-first-letter-of-each-word-in-a-string.aspx

python單詞首字母大寫

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

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

相關文章

matlab中求模最大,matlab求取模極大值時出錯

本帖最后由 Nate_ 于 2016-4-17 15:57 編輯points1024 時,有波形輸出,但信號有5438個點。改為5438就不行。主程序:%小波模極大值重構是采用的交替投影法close all;points5438; level4; sr360; num_inter6; wfdb4;%所處理數據的…

stl向量_如何檢查C ++ STL中向量中是否存在元素?

stl向量Given a vector and an element to be searched in the vector. 給定一個向量和要在向量中搜索的元素。 To check whether an elements exists in a vector or not – we use find() function. find() function takes 3 arguments. 要檢查向量中是否存在元素 –我們使用…

java socket如何請求485協議_javaSE第十五部分 網絡編程(1)Socket和ServerSocket

網絡編程基礎知識C/S結構:全稱為Client/Server結構,是指客戶端和服務器結構。常見程序有QQ、迅雷等軟件。B/S結構:全稱為Browser/Server結構,是指瀏覽器和服務器結構。常見瀏覽器有谷歌、火狐等。兩種架構各…

【分享】linux下u盤使用

2019獨角獸企業重金招聘Python工程師標準>>> linux下u盤使用 方案一: Linux不像Windows一樣,接上新硬件后可以自動識別,在Linux下無法自動識別新硬件的,需要手動去識別。USB移動存儲設備通常被識別為sda1,…

kotlin中判斷字符串_Kotlin程序刪除字符串中所有出現的字符

kotlin中判斷字符串Given a string and a character, we have to remove all occurrences of the character in given string. 給定一個字符串和一個字符,我們必須刪除給定字符串中所有出現的字符。 Example: 例: Input:string "includeHelp Del…

Java9中使用jpa,jpa – eclipselink在Java 9上使用final字段進行靜態編織

我有一些JPA注釋字段,如下所示:Column(name "SOME_FIELD", updatable false, nullable false)private final String someField;當實體插入數據庫時??,這些字段存儲在數據庫中.它們無法進一步更新.對于Java編程語言,可以將這些字段視為final.使用Ecli…

python語言程序設計及醫學應用_Python語言程序設計(高等學校計算機專業規劃教材)...

第1章Python語言概述/1 1.1Python語言的發展1 1.1.1Python的起源1 1.1.2Python的發展2 1.2Python語言的特點2 1.2.1Python的特性2 1.2.2Python的缺點4 1.2.3Python與其他語言的比較5 1.3簡單的Python程序介紹5 1.4Python的程序開發工具8 1.4.1Python的版本選擇8 1.4.2Python的安…

swift 3.0 中使用 xib

文章寫于2016年9月底,Xcode 8,swift 3.0真是蛋疼,折騰了很長時間,試了網上很多教程,結果又莫名的可以了! 1.方法和OC中一樣 將一個xib文件和一個ViewController類進行關聯的幾步操作: command &…

數字圖像處理圖像反轉的實現_使用8086微處理器反轉16位數字

數字圖像處理圖像反轉的實現Problem statement: 問題陳述: Write an assembly language program in 8086 microprocessor to reverse 16 bit number using 8 bits operation. 在8086微處理器中編寫匯編語言程序,以使用8位操作反轉16位數字。 Example: …

php猴子找大王算法,教程方法;php實現猴子選大王問題算法實例電腦技巧-琪琪詞資源網...

琪琪詞資源網-教程方法;php實現猴子選大王問題算法實例電腦技巧,以下是給大家帶來的教程方法;php實現猴子選大王問題算法實例,大家可以了解一下哦!下面為你介紹php實現猴子選大王問題算法實例。本文實例講述了php實現猴子選大王問題算法。分享給大家供大…

numpy 歸一化_NumPy 數據歸一化、可視化

僅使用 NumPy,下載數據,歸一化,使用 seaborn 展示數據分布。下載數據import numpy as npurl https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.datawid np.genfromtxt(url, delimiter,, dtypefloat, usecols[1])僅提取…

java虛擬機規范閱讀(三)異常

Java虛擬機里面的異常使用Throwable或其子類的實例來表示,拋異常的本質實際上是程序控制權的一種即時的、非局部(Nonlocal)的轉換——從異常拋出的地方轉換至處理異常的地方。絕大多數的異常的產生都是由于當前線程執行的某個操作所導致的&am…

puppeteer api_使用Node.js和puppeteer API從URL創建PDF文件

puppeteer apiWe will continue using Node.js and puppeteer which is a node library. As we saw in our last article, Puppeteer is a Node library developed by Google and provides a high-level API for developers. 我們將繼續使用Node.js和puppeteer(這是一個節點庫)…

python線程同步鎖_[python] 線程間同步之Lock RLock

為什么需要同步 同樣舉之前的例子,兩個線程分別對同一個全局變量進行加減,得不到預期結果,代碼如下: total 0 def add(): global total for i in range(1000000): total 1 def desc(): global total for i in range(1000000): t…

servlet的由來

2019獨角獸企業重金招聘Python工程師標準>>> 動靜態網頁技術 首先說下訪問網頁的大概過程: 你在瀏覽器中輸入網址,按下enter鍵,此時瀏覽器代你做了很多事,簡要說為:將你輸入的這個網址作為目的地參數&#…

php header 文件大小,php獲取遠程文件大小及信息的函數(head_php

php獲取遠程文件大小及信息的函數(header頭信息獲取)阿里西西Alixixi.com開發團隊在做一個客戶系統時,需要做遠程下載的功能,并實時顯示進度條效果。所以,需要預先讀取遠程文件的大小信息,然后做為實時下載進度條的參數。功能函數…

Java ObjectInputStream readUnsignedShort()方法(帶示例)

ObjectInputStream類readUnsignedShort()方法 (ObjectInputStream Class readUnsignedShort() method) readUnsignedShort() method is available in java.io package. readUnsignedShort()方法在java.io包中可用。 readUnsignedShort() method is used to read 2 bytes (i.e. …

python中info的用法_Python pandas.DataFrame.info函數方法的使用

DataFrame.info(self, verboseNone, bufNone, max_colsNone, memory_usageNone, null_countsNone) [source] 打印DataFrame的簡要摘要。 此方法顯示有關DataFrame的信息,包括索引dtype和列dtype,非空值和內存使用情況。 參數:verbose &#x…

第四次作業 孫保平034 李路平029

用C編寫一個學生成績管理系統 1、可以實現以下功能&#xff1a; cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 1.增加學生成績 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl; 2、用鏈表存儲信息 * 程序頭部的注釋結束 3、約定的規范&#xff1a; 1界面設計簡介&#xff0c;人性化…

php serialize error at offset,PHP Notice: unserialize(): Error at offset XX of XX bytes

之前同事在本地開發的時候&#xff0c;出現一個錯誤&#xff0c;如下圖所示&#xff1a;字面意思就是反序列化錯誤&#xff0c;由此bug引申出來序列化和反序列化得應用&#xff0c;以及php array當key為string類型的數字值時&#xff0c;會發生什么情形。先來看序列化$str [1 …