Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python programs

Python | Program to print words with their length of a string



Here, we will learn how to print the length of the words from a string in Python? To extract the words from the string, we will use String.split() method and to get word’s length, we will use len() method.
Submitted by IncludeHelp, on July 27, 2018

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

Example:

    Input:
    str = "Hello World How are you?"

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

String.split() Method

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() Method Syntax:

 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".

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 )





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.