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 length
def splitString (str):
# split the string by spaces
str = str.split (' ')
# iterate words in string
for 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 | 程序以字符串长度打印单词相关推荐

  1. 在Python中查找字符串长度

    介绍 (Introduction) In this tutorial, we are going to discuss how we can find string length in Python. ...

  2. python如何限制字符串长度_python如何修改字符串长度

    python如何修改字符串长度 发布时间:2020-09-23 17:16:18 来源:亿速云 阅读:95 作者:Leah 今天就跟大家聊聊有关python如何修改字符串长度,可能很多人都不太了解,为 ...

  3. 不使用strcpy函数,编程实现把b字符串复制到a中的程序(字符串长度限制在100个字符以内)。以下是部分代码,请完成并提交

    不使用strcpy函数,编程实现把b字符串复制到a中的程序(字符串长度限制在100个字符以内).以下是部分代码,请完成并提交. #include <stdio.h> int main(){ ...

  4. python如何判断字符串长度_Python如何查找字符串的长度?(代码示例)

    在Python中字符串是Unicode代码点的不可变序列.给定一个字符串,我们如何查找它的长度?本篇文章就来给大家介绍在Python中查找字符串长度的四种方法,希望对大家有所帮助.[相关视频教程推荐: ...

  5. python计算单词长度_Python - 按长度打印单词

    首先根据长度对单词进行排序,然后再次使用itertools.groupby对它们进行分组: >>> from itertools import groupby >>> ...

  6. 如何使得自己的Python程序每行长度小于80个字符?

    ➤01 Google编程规范 在今日头条中一篇 Python程序开发:一份来自谷歌的Python编写风格规范 的短文给出了Google给出的针对于Python编程的编程规范.其中建议"每行程 ...

  7. python字符串查找重复项,Python程序查找字符串中所有重复的字符

    在本教程中,我们将学习如何在字符串中查找所有重复值.我们可以在Python中以不同的方式进行操作.让我们一一探讨. 我们要编写的程序的目的是查找字符串中存在的重复字符.例如,我们有一个字符串tutor ...

  8. Python程序计算给定文本中单词的出现

    Given a text (paragraph) and a word whose occurrence to be found in the text/paragraph, we have to f ...

  9. python如何限制字符串长度_Python uuid4,如何限制唯一字符的长度 - python

    在Python中,我正在使用uuid4()方法创建唯一的字符集.但是我找不到将其限制为10或8个字符的方法.有什么办法吗? uuid4() ffc69c1b-9d87-4c19-8dac-c09ca8 ...

最新文章

  1. UnixLinux大学教程 读书笔记【1-3章】
  2. win8: hello gril
  3. CenOS 配置C/C++语言
  4. linux raw格式改名img,4款Linux下的RAW格式图片编辑软件
  5. linux学习笔记1:基础知识
  6. 把uliweb项目变成可安装的python包
  7. 一篇相当不错的function入门文章
  8. 剑指offer全书题解 (Python)【更新完毕】
  9. 有序列表ol与无序列表ul用法
  10. 别怨自己命不好,先看看“厚德载物”你有几德?
  11. JVM监控及诊断工具GUI篇之Visual VM
  12. 基于Java+SpringBoot+vue+elementui药品商城采购系统详细设计实现
  13. 指导CoVaR,基于Copula、GARCH、DCC、分位数回归、藤VineCopula
  14. java 微信网页授权_java微信网页授权获取openid(第十四课)
  15. 个人投资课 张潇雨_张潇雨《个人投资课》学习笔记
  16. 蒙特卡洛_蒙特卡洛辍学
  17. iOS视频编辑SDK
  18. 在线教育直播系统 一对一在线直播平台解决方案
  19. 学python能干嘛-学 Python 都用来干嘛的?
  20. 重庆医科大学计算机考研分数线,重庆医科大学2019年考研复试分数线已公布

热门文章

  1. 前端 PDFObject.embed预览PDF,另类方式隐藏工具条样例
  2. oracle 索引invisible,Oracle index unusable和invisible的区别
  3. linux没有root密码xshell,LINUX终端免密登陆(以Xshell为例)
  4. 《React Native 精解与实战》书籍连载「React Native 网络请求与列表绑定」
  5. 使用 Web Tracing Framework 分析富 JS 应用的性能
  6. 一步步构建大型网站架构 [转]
  7. (转)模拟鼠标/键盘
  8. 程序员的幸福感和颈椎病
  9. Deepin安装Curl的方法
  10. 内存泄露从入门到精通三部曲之排查方法篇