604 views|8 replies

539

Posts

3

Resources
The OP
 

Reading experience of the book "Python Programming Quick Start (2nd Edition)" 1. String Operation [Copy link]

This post was last edited by xinmeng_wit on 2024-4-24 21:40

1. Book Introduction

This book is a practical guide to Python programming for beginners. In fact, not only beginners, but also students who are familiar with Python and want to automate their daily work should read this book, which introduces a lot of Python programming skills and cases.

This book is divided into two parts. The first part introduces basic Python programming concepts (about 6 chapters), and the second part introduces some different tasks and automates work through Python programming (about 14 chapters), which is the focus of the book.

The first part, Python basics, takes up about 1/4 of the book, and the remaining 3/4 is about how to use Python to automate work, which is what attracts me to this book.

Chapter 1 talks about Python basics. Expressions, variable types, strings, etc.

Chapter 2 talks about Python's control flow;

Chapter 3 talks about functions;

Chapter 4 is about lists;

Chapter 5 talks about dictionaries;

Chapter 6 is about string operations.

The first five chapters are all about the basics of Python data types and syntax, which are relatively simple. For students with some programming knowledge, they only need to go through them briefly.

After reading Chapter 6, you will find that Python's string processing is very flexible and provides quite a lot of string processing methods, such as string indexing, slicing, searching, and conversion to lists or tuples.

Therefore, this article summarizes and extracts the string-related content explained in this book.

String Operations

1. String definitions can use single quotes (such as 'Hello World'), double quotes (such as "Hello World"), or triple quotes (such as '''Hello World''');

2. Available escape characters in Python

The following example:

3. Triple quotes can define multi-line strings and can also be used for comments

Multi-line string example:

Examples for annotations:

4. Strings can be indexed by subscript

The following example:

5. Strings can be sliced

The following example:

6. Strings can be matched

7. Strings can be placed inside other strings

Another way to write it:

The third way to write:

8. String methods upper(), lower(), isupper(), islower()

The upper() method converts all letters of a character to uppercase

The lower() method converts all letters in a string to lowercase

isupper() is used to check whether the string is all uppercase, and islower() is used to check whether the string is all lowercase.

9. isX() string method

10. Determine the start and end methods of a string: startswith(), endswith()

11. The join() method is used to connect a list of strings into a string

12. The split() method splits a string into a list of characters

13. The partition() method is used to divide a string into tuples (3 segments)

14. Use rjust(), ljust() and center() to align text

rjust(): right alignment

ljust(): Left alignment

center(): center alignment

15. Use strip(), rstrip() and lstrip() methods to remove whitespace characters

Whitespace characters are spaces, tabs, and newlines

strip(): remove all whitespace characters

rstrip(): remove whitespace characters on the right

lstrip(): remove blank characters on the left

16. Use ord() and chr() to convert between characters and numbers

Numbers are Unicode code points

In addition to the above string processing methods, there is also a more powerful operating system clipboard processing library pyperclip, which can copy and paste text on the system clipboard.

There are two examples in the book, one is to use the pyperclip library to quickly reply to messages, and the other is to quickly add specified content to text.

Quick reply message code:

#! python3
# mclip.py - A multi-clipboard program

TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
        'busy': """Sorry, can we do this later this week or next week?""",
        'upsell': """Would you consider making this a monthly donation?"""}

import sys, pyperclip

if len(sys.argv) < 2:
    print('Usage: py mclip.py [keyphrase] - copy phrase text')
    sys.exit()

keyphrase = sys.argv[1]  # first command line arg is the keyphrase

if keyphrase in TEXT:
    pyperclip.copy(TEXT[keyphrase])
    print('Text for ' + keyphrase + ' copied to clipboard.')
else:
    print('There is no text for ' + keyphrase)

operation result:

After execution is completed, the text "Yes, I agree. That sounds fine to me." will be copied to the system clipboard and then pasted directly to where it is needed. This way, you don't have to type the text by hand, and you can quickly copy the text.

Quickly add the specified content code to the text:

#! python3
# bulletPointAdder.py - Adds wikipedia bullet points to the start
# of each line of text on the clipboard

import pyperclip

text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):  # loop through all indexes for "lines" list
    lines[i] = '*' + lines[i] # add star to each string in "lines" lists
text = '\n'.join(lines)
pyperclip.copy(text)

The purpose of this code is to read the contents of the system clipboard and then add an asterisk (*) in front of each paragraph of text before copying it to the clipboard, so that the text can be quickly changed.

Through the study of this chapter, you have mastered various string processing methods, which are very useful. I believe that they will definitely be used in the later practical courses, and they will achieve twice the result with half the effort.

Latest reply

It's good to have a basic understanding. Although it is similar to other languages, there are still some small differences.   Details Published on 2024-4-26 08:22
 
 

736

Posts

4

Resources
2
 

The string-related operations of Python language shared by the host are very detailed and suitable for beginners.

Comments

My friend, why did you get up so early? At this time, I am still not awake.  Details Published on 2024-4-25 09:00
My friend, why did you get up so early? At this time, I am still not awake.  Details Published on 2024-4-25 08:49
 
 
 

1129

Posts

1

Resources
3
 
chejm posted on 2024-4-25 05:42 The string-related operation methods shared by the host in Python are very detailed and suitable for beginners

My friend, why did you get up so early? At this time, I am still not awake.

 
 
 

1129

Posts

1

Resources
4
 

The author of the post is indeed a careful person. This article is very lengthy and explains all the strings clearly.

Some of them are the first time I see them (paste part)

Like the OP!

Comments

Yes, I saw a lot of the content in the book for the first time, and I thought it was very useful, so I listed it here.  Details Published on 2024-4-25 09:00
 
 
 

539

Posts

3

Resources
5
 
chejm posted on 2024-4-25 05:42 The content of the string-related operations in Python shared by the host is very detailed and suitable for beginners

Yes, this book is very thorough and suitable for beginners.


 
 
 

539

Posts

3

Resources
6
 
hellokitty_bean posted on 2024-4-25 08:51 The author is indeed a careful person. This article is very comprehensive and explains all the strings clearly. Some of them are the first time I have seen them (paste part...

Yes, I saw a lot of the content in the book for the first time, and I thought it was very useful, so I listed it here.


 
 
 

6075

Posts

6

Resources
7
 

This is a direct fusion. I am still looking at the previous variable control flow, although it is similar to many other languages.

Comments

I just quickly went through it before, and I want to focus on the application part later.  Details Published on 2024-4-25 19:20
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

539

Posts

3

Resources
8
 
Qintianqintian0303 posted on 2024-4-25 11:45 This is a direct fusion. I am still looking at the previous variable control flow, although it is similar to many other languages

I just quickly went through it before, and I want to focus on the application part later.

Comments

It's good to have a basic understanding. Although it is similar to other languages, there are still some small differences.  Details Published on 2024-4-26 08:22
 
 
 

6075

Posts

6

Resources
9
 
xinmeng_wit posted on 2024-4-25 19:20 I just quickly went through it before, and I want to focus on the application part later

It's good to have a basic understanding. Although it is similar to other languages, there are still some small differences.

Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list