-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_string.py
40 lines (27 loc) · 886 Bytes
/
python_string.py
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
# Hello World program in Python
print("Example of Python Strings\n")
# Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a)
print(a[0])
print(a[1])
print(a[2])
print(a[3])
# Substring. Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])
# The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
# The len() method returns the length of a string:
a = "Hello, World!"
print(len(a))
# The lower() method returns the string in lower case:
a = "HelLo, WoRld!"
print(a.lower())
# The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
# The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))