Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Python for Programmers

You're reading from   Python for Programmers A Comprehensive Guide for Intermediate to Advanced Python Programmers and Developers

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Mercury_Learning
ISBN-13 9781836645030
Length 293 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Mercury Learning and Information Mercury Learning and Information
Author Profile Icon Mercury Learning and Information
Mercury Learning and Information
Oswald Campesato Oswald Campesato
Author Profile Icon Oswald Campesato
Oswald Campesato
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface
1. Chapter 1: Introduction to Python 2. Chapter 2: Conditional Logic in Python FREE CHAPTER 3. Chapter 3: Data Structures in Python 4. Chapter 4: Strings and Arrays 5. Chapter 5: Built-In Functions and Custom Classes 6. Chapter 6: Recursion and Combinatorics 7. Index
Appendix: Introduction to Pandas

THE JOIN() FUNCTION

Another way to remove extraneous spaces is to use the join() function:

text1 = '   there are   extra  spaces   '

print('text1:',text1)


text2 = ' '.join(text1.split())

print('text2:',text2)


text2 = 'XYZ'.join(text1.split())

print('text2:',text2)

The split() function “splits” a text string into a set of words, and also removes the extraneous white spaces. Next, the join() function “joins” together the words in the string text1, using a single white space as the delimiter. The last code portion of the preceding code block uses the string XYZ as the delimiter instead of a single white space. The output of the preceding code block is as follows:

text1:   there are   extra   spaces

text2: there are extra spaces

text2: thereXYZareXYZextraXYZspaces...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python for Programmers
You have been reading a chapter from
Python for Programmers
Published in: Aug 2024
Publisher: Mercury_Learning
ISBN-13: 9781836645030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime
Modal Close icon
Modal Close icon