Thứ Hai, Tháng Một 30, 2023
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
No Result
View All Result
Home Tech

Chapter 3: Strings – Python Basics

30 Tháng Chín, 2022
in Tech
0
Chapter 3: Strings – Python Basics
585
SHARES
3.2k
VIEWS
Share on FacebookShare on Twitter

Các bài viết liên quan:

How to get travel insurance

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023
5 Best Software to Stream Games

5 Best Software to Stream Games

2 Tháng Một, 2023
IBM Bridge To Cloud For Power

IBM Bridge To Cloud For Power- Everything You Should Know

2 Tháng Một, 2023
Top 10 CRM Software For Construction 

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
What Is IBM Software

What Is IBM Software? 4 Business Segments at IBM You Should Know

26 Tháng Mười Hai, 2022

A string is a set of characters consisting of letters or letters and numbers. In everyday life, we encounter “strings” as often as the words in this article. String example:

  • "Đây là Website Natuts.com"
  • "Thứ 2 là ngày đầu tuần"
  • "1 2 3 5 anh đánh rơi nhịp nào không"

How to create a string in Python

In Python, strings are defined in parentheses or quotes or 3 single quotes:

Example of creating a string with parentheses:

s1 = 'Đây là Website Natuts.com'

Example of string definition using Quotes:

s2 = "Thứ 2 là ngày đầu tuần"

Example of string definition using 3 single quotes. With 3 ” ” you can write string on multiple lines.

s3 = '''Cộng hòa xã hội chủ nghĩa Việt Nam
Độc lập - Tự do - Hạnh phúc'''

I'm in python

Some String Functions in Python

upper, lower Handling uppercase and lowercase printing
rjust Right alignment
ljust Aligns the left
center Center align
strip Remove excess white space
startswith Check if String must start with a character?
endswith Check if a String ends in a character?
count Count occurrences in String
find Search for Substring
format String Format
len() Returns the number of characters in the string, using index to get the characters out: str[index]

Exercise 1: Convert all lowercase letters to uppercase.

I will create a variable s1 with input input().

After turning s1 If there is a value entered from the keyboard, we will convert it to uppercase using the function upper(). Then the formula will be s1.upper(). Finally, use the command print() to print to the screen.

Chapter 3: Strings - Python Basics 13

Similarly, the function lower() will convert uppercase to lowercase. Do the opposite yourself.

Exercise 2: Left align the string “Natuts” with 5 *

Note if the number of characters you want to insert is less than the original String, nothing will change.

By default the character will be a space, you can change to other characters like *,$,@ or any character on keyboard

I will create a variable s1 contains string Natuts.

We have Natuts there are 9 characters, so to insert the next character, the length must start from 10. So to add 5 * characters, we will have the length formula as 10+5 = 15.

Move to the left, we will use the function ljust(độ dài, ký tự)so to align the left margin with 10 * for the string s1 will s1.ljust(15,*) Finally, use the command print() to print to the screen.

Chapter 3: Strings - Python Basics 14

Similarly, try your hand at right and center alignment in Python!

Exercise 3: Remove redundant spaces at the beginning and end of lines using

Python supports a string normalization function, which removes excess spaces at the beginning and end of a string. You can use the function strip() to do this.

Chapter 3: Strings - Python Basics 15

Exercise 3: Check if there is an Anony in the string “Handsome Natuts”?

Python has 2 functions startswith() and endswith() to check if there is a substring in an existing string? If yes return Trueotherwise return False

Note: These 2 functions are case sensitive.

Chapter 3: Strings - Python Basics 16

Try the above post with the function endswith() Please.

Exercise 4: Find the position of the letter V in the string “Natuts”

Python has a function find() Used to find the first position of a character in a string. Note: position in Python starts from 0 Please.

I have a chain s1 = "Natuts". To find the first position where the letter V appears, we use the command find("v"). Then use the command print() to print out the location.

Chapter 3: Strings - Python Basics 17

So the result will be the first V position in the Natuts string: 5

Chapter 3: Strings - Python Basics 18

Exercise 4: Find the last position of the letter “n” in the string “Natuts”

In python we have the function rfind(), find the last position found. To solve this problem, do the following:

Create variable s1 containing string Natuts:

s1 = "Natuts"

To print the last n in the string s1 we use the command print() combine s1.rfind("n").

The result is print(s1.rfind("n"))

Since Python calculates from position 0, the last n will be counted as 3

Chapter 3: Strings - Python Basics 19

Chapter 3: Strings - Python Basics 20

The case if there are no words in the string will return -1.

For example, you need to find the word X in the chain “Natuts”.

Since X does not appear in Natuts, the result will be returned -1.

Exercise 5: Calculate the number of occurrences of the word “no” in the string “Natuts”

I have a function count() to count the number of occurrences of a substring in the parent string. We do the following:

s1 = "Natuts"

print(s1.count("no")

The result will be: first. Because the string appears once, the word no => AnonyViet

Chapter 3: Strings - Python Basics 21

Exercise 6: Print the word Viet in the string “Natuts”

In python we can get substring using distance from position A to position B according to the formula [A:B]

  • After the : if nothing is taken the last position
  • In front of the : if nothing is taken the first position

In the string “Natuts” we have a position from 0. To split the string you do the following:

s1 = "Natuts"

print(s1[5:]) #do the letter t in the last position up after the : we leave it blank.

Result: Write

Chapter 3: Strings - Python Basics 22

The other two string splitting and string concatenation functions we will see in Chapter 4, will then use a loop to execute these two commands.

Previous Post

How to Permanently Delete a Roblox Account (2022)

Next Post

Taiwan commissions a new amphibious ship

Megusta

Megusta

Related Posts

5 Best Software to Stream Games

5 Best Software to Stream Games

2 Tháng Một, 2023
Top 10 CRM Software For Construction 

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
Instruction how to use OBS streaming software

Features, settings and how to use OBS streaming software through 9 simple steps

25 Tháng Mười Hai, 2022
What is Trans woman?  What is Transgender Women?

What is Trans woman? What is Transgender Women?

23 Tháng Mười Hai, 2022
Christmas gift: Genuine Windows 10 Pro for only $6.63 and Office 2021 for $14.22

Christmas gift: Genuine Windows 10 Pro for only $6.63 and Office 2021 for $14.22

22 Tháng Mười Hai, 2022
How to get 50 free coins of SkyJoy App to redeem

How to get 50 free coins of SkyJoy App to redeem

21 Tháng Mười Hai, 2022
Load More
Next Post
Taiwan commissions a new amphibious ship

Taiwan commissions a new amphibious ship

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Bài viết mới

How to get travel insurance
Đời sống

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly
Phần mềm

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023
5 Best Software to Stream Games
Software

5 Best Software to Stream Games

2 Tháng Một, 2023
IBM Bridge To Cloud For Power
Software

IBM Bridge To Cloud For Power- Everything You Should Know

2 Tháng Một, 2023
Top 10 CRM Software For Construction 
Tech

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
What Is IBM Software
Software

What Is IBM Software? 4 Business Segments at IBM You Should Know

26 Tháng Mười Hai, 2022
W3Schools

Ads

Contact: [email protected]

DMCA.com Protection Status

Categories

  • Android
  • Cạm bẫy tâm lí
  • Chưa được phân loại
  • Đồ họa
  • Đời sống
  • Gen Z
  • Health
  • iOS
  • Kĩ năng mềm
  • News
  • Nhà mạng
  • Phần mềm
  • Phần mềm đồ họa
  • Review sách
  • Software
  • Tech
  • Thiết kế ảnh
  • Thiết kế video
  • Thủ thuật
  • Travel
  • Văn hóa Nam Bộ
  • Văn học
  • Window

Browse by Tag

ai là triệu phú android Apple browser Bullet Journal bản thân chai pin Chỉnh ảnh data domain download fshare game game show giả lập màu hosting IKEA ios khuyến mãi kinh doanh kiến thức kiểm tra pin messenger miễn phí mua sắm Máy ảnh mạng network nghệ thuật ngôn ngữ nhà Trần pin laptop quảng cáo tiếng anh trạng thái Trần Thủ Độ tên miền tắt hoạt động từ vựng video viettel window 10 word zalo Đơn giản

Recent News

How to get travel insurance

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023

Trang tin nóng hổi - vừa thổi vừa xem

No Result
View All Result
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z

Trang tin nóng hổi - vừa thổi vừa xem