日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python | 程序以字符串长度打印单词

發布時間:2023/12/1 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python | 程序以字符串长度打印单词 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Given a string and we have to split the string into words and also print the length of the each word in Python.

給定一個字符串,我們必須將字符串拆分為單詞,并在Python中打印每個單詞的長度。

Example:

例:

Input:str = "Hello World How are you?"Output:Hello ( 5 )World ( 5 )How ( 3 )are ( 3 )you? ( 4 )

String.split() Method

String.split()方法

To split string into words, we use split() method, it is an inbuilt method which splits the string into set of sub-string (words) by given delimiter.

要將字符串拆分為單詞,我們使用split()方法 ,這是一個內置方法 ,它通過給定的定界符將字符串拆分為子字符串(單詞)的集合。

split() Method Syntax:

split()方法語法:

String.split(delimiter)

Explanation:

說明:

For example, there is a string str = "ABC PQR XYZ" and we want to split into words by separating it using space, then space will be delimiter here. To split the string to words, the statement will be str.split(" ") and then output will be "ABC" "PQR" "XYZ".

例如,有一個字符串str =“ ABC PQR XYZ” ,我們想通過使用空格將其拆分為多個單詞,那么此處的空格將成為定界符。 要將字符串拆分為單詞,該語句將為str.split(“”) ,然后輸出為“ ABC” “ PQR” “ XYZ” 。

Program:

程序:

# Function to split into words # and print words with its lengthdef splitString (str):# split the string by spacesstr = str.split (' ')# iterate words in stringfor words in str:print words," (", len (words), ")"# Main code # declare string and assign value str = "Hello World How are you?"# call the function splitString(str)

Output

輸出量

Hello ( 5 )World ( 5 )How ( 3 )are ( 3 )you? ( 4 )

翻譯自: https://www.includehelp.com/python/print-words-with-their-length-of-a-string.aspx

總結

以上是生活随笔為你收集整理的Python | 程序以字符串长度打印单词的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。