387 views|3 replies

88

Posts

0

Resources
The OP
 

"Python Programming Quick Start - Automate Tedious Work" 5. String Operation [Copy link]

This post was last edited by Haoyueguangxifeiziming on 2024-6-6 03:19

5. String Operations

1. Creating a string

str1 = "Hello, EEWorld! This is a Python string."

(II) Connection string

str1 = "Hello" + " " + "EEWorld"

print(str1)

# Output: Hello EEWorld

(III) Cropping operation

substring = str1[6:12]

print(substring)

# Output: EEWorld

(IV) Finding substrings

index = str1.find("EEWorld")

print(index)

# Output: 6

(V) Replace substring

replace = str1.replace("EEWorld", "World")

print(replace)

# Output: Hello, World! This is a Python string.

(VI) Splitting a string

split_string = str1.split(" ")

print("Split string:", split_string)

# Output: [Hello, EEWorld!, 'This', 'is','a','Python','string.']

(VII) Case conversion

lower_string = str1.lower()

upper_string = str1.upper()

print(lower_string)

print(upper_string)

# Output: [hello, eeworld!, 'this', 'is','a','python','string.']

# Output: [HELLO, EEWORLD!, 'THIS', 'IS','A','PYTHON','STRING.']

(VIII) Determine whether a string starts or ends with a specific substring

start = str1.startswith("Hello")

end = str1.endswith("string.")

print(start)

print(end)

# Output: True

# Output: True

9. Determine whether a string contains only letters or numbers

is_alpha = str1.isalpha()

is_num = str1.isnumeric()

print(is_alpha)

print(is_num)

# Output: True

# Output: False

Latest reply

Thank you so much for sharing.   Details Published on 2024-6-6 10:07
 
 

6822

Posts

11

Resources
2
 

I have used some of these operations before, but for some of them, such as string.startswith which starts with something, I have heard of them for the first time. Thanks for sharing.

Comments

If you want C/C++, you can only modify the character matching function to achieve it. Python independently encapsulates some small function functions, and you can directly call them by checking the manual. It is indeed much more convenient  Details Published on 2024-6-6 08:55
 
 
 

88

Posts

0

Resources
3
 
lugl4313820 posted on 2024-6-6 07:42 I have used some of these operations before, but I have never heard of some of them, such as string.startswith, which starts with something. Thanks for sharing.

If you want C/C++, you can only modify the character matching function to achieve it. Python independently encapsulates some small function functions, and you can directly call them by checking the manual. It is indeed much more convenient

Comments

Thank you very much for sharing.  Details Published on 2024-6-6 10:07
 
 
 

6822

Posts

11

Resources
4
 
The bright moonlight is not self-evident. Published on 2024-6-6 08:55 If you want C/C++, you can only modify the character matching function to achieve it. Python independently encapsulates some small function functions. You can just call them directly by checking the manual...

Thank you so much for sharing.

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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