Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7d4c7c

Browse files
authored
Create les4_using_strings.py
Using Strings Strings are Arrays. Like many other popular programming languages, strings in Python are arrays of bytes representing Unicode characters.
1 parent 6c8ab96 commit e7d4c7c

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

‎les4_using_strings.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#https://newdigitals.org/2024/01/23/basic-python-programming/#using-strings
2+
#Assigning a single-line string to a variable
3+
a = "Hi!"
4+
print(a)
5+
#Hi!
6+
#Assigning a multi-line string to a variable
7+
a = """Lorem ipsum dolor sit amet,
8+
consectetur adipiscing elit,
9+
sed do eiusmod tempor incididunt
10+
ut labore et dolore magna aliqua."""
11+
print(a)
12+
#Lorem ipsum dolor sit amet,
13+
#consectetur adipiscing elit,
14+
#sed do eiusmod tempor incididunt
15+
#ut labore et dolore magna aliqua.
16+
#Loop through the letters in the word "banana":
17+
for x in "banana":
18+
print(x)
19+
#b
20+
#a
21+
#n
22+
#a
23+
#n
24+
#a
25+
#The len() function returns the length of a string
26+
a = "Hello, World!"
27+
print(len(a))
28+
#13
29+
#To check if a certain phrase or character is present in a string, we can use the keyword in:
30+
txt = "The best things in life are free!"
31+
print("free" in txt)
32+
#True
33+
#Check if "expensive" is NOT present in the following text
34+
txt = "The best things in life are free!"
35+
print("expensive" not in txt)
36+
#True
37+
#You can return a range of characters by using the slice syntax
38+
b = "Hello, World!"
39+
print(b[2:5])
40+
#llo
41+
#Get the characters from the start to position 5 (not included):
42+
b = "Hello, World!"
43+
print(b[:5])
44+
#Hello
45+
#Get the characters from position 2, and all the way to the end:
46+
b = "Hello, World!"
47+
print(b[2:])
48+
#llo, World!
49+
#Use negative indexes to start the slice from the end of the string:
50+
#From: "o" in "World!" (position -5)
51+
#To, but not included: "d" in "World!" (position -2):
52+
b = "Hello, World!"
53+
print(b[-5:-2])
54+
#orl
55+
#The upper() method returns the string in upper case:
56+
a = "Hello, World!"
57+
print(a.upper())
58+
#HELLO, WORLD!
59+
#The lower() method returns the string in lower case:
60+
a = "Hello, World!"
61+
print(a.lower())
62+
#hello, world!
63+
#The strip() method removes any whitespace from the beginning or the end:
64+
a = " Hello, World! "
65+
print(a.strip()) # returns "Hello, World!"
66+
#Hello, World!
67+
#The replace() method replaces a string with another string:
68+
a = "Hello, World!"
69+
print(a.replace("H", "J"))
70+
#Jello, World!
71+
#The split() method splits the string into substrings if it finds instances of the separator:
72+
a = "Hello, World!"
73+
print(a.split(",")) # returns ['Hello', ' World!']
74+
#['Hello', ' World!']
75+
#Merge variable a with variable b into variable c:
76+
a = "Hello"
77+
b = "World"
78+
c = a + b
79+
print(c)
80+
#HelloWorld
81+
#To add a space between them, add a " ":
82+
a = "Hello"
83+
b = "World"
84+
c = a + " " + b
85+
print(c)
86+
#Hello World

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /