FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_programming_/string_in_python.py at main · machphy/python_programming_ · GitHub
machphy
/
python_programming_
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
4
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python_programming_
/
string_in_python.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (31 loc) · 1.54 KB
Breadcrumbs
python_programming_
/
string_in_python.py
Copy path
File metadata and controls
48 lines (31 loc) · 1.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Example of string methods
# Initial string
text
=
" Hello World Python "
# Convert to lowercase
print
(
text
.
lower
())
# Output: " hello world python "
# Convert to uppercase
print
(
text
.
upper
())
# Output: " HELLO WORLD PYTHON "
# Capitalize each word
print
(
text
.
title
())
# Output: " Hello World Python "
# Strip leading and trailing spaces
print
(
text
.
strip
())
# Output: "Hello World Python"
# Replace 'Python' with 'Programming'
print
(
text
.
replace
(
"Python"
,
"Programming"
))
# Output: " Hello World Programming "
# Split the string into a list of words
print
(
text
.
split
())
# Output: ['Hello', 'World', 'Python']
# Join a list of words into a single string with spaces
words
=
[
'Hello'
,
'World'
,
'Python'
]
print
(
" "
.
join
(
words
))
# Output: "Hello World Python"
# Find the position of 'World'
print
(
text
.
find
(
"World"
))
# Output: 11
# Check if the string starts with ' Hello'
print
(
text
.
startswith
(
" Hello"
))
# Output: True
# Check if the string ends with 'Python'
print
(
text
.
endswith
(
"Python "
))
# Output: True
# Check if the string contains only digits
print
(
"12345"
.
isdigit
())
# Output: True
# Check if the string contains only alphabetic characters
print
(
"Hello"
.
isalpha
())
# Output: True
### Key Points
# - **Strings are immutable:** Methods that modify a string return a new string rather than changing the original string.
# - **Method chaining:** You can chain methods together for more complex operations. For example, `text.strip().upper().replace("WORLD", "EVERYONE")`.
Back
|
FazBrowse Home
|
New Git URL