-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_stringExample.py
More file actions
executable file
·35 lines (26 loc) · 1.01 KB
/
04_stringExample.py
File metadata and controls
executable file
·35 lines (26 loc) · 1.01 KB
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
#!/usr/bin/env python3
"""
Examples for string handling
https://docs.python.org/3/library/string.html
Jens Dede, 2019, jd@comnets.uni-bremen.de
"""
s = "This is my string with some meaningful text"
print(s) # Print string
print(s.split(" ")) # Separate by spaces
print(s.replace("s", "x")) # Replace characters
print(s.lower()) # To lower case
print(s.upper()) # To upper case
print(s.find("m")) # Find a character in the string
# You can access parts of a string (data structure) as follows:
print(s[:4]) # Print part of the string
# Explanation:
# s[:4] = All characters till the 4th
# s[1:4] = Character 2 till the 4th (counting starts with 0!)
# s[-4:] = Last four characters
# s[::2] = Every 2nd character
if s[:4] == "This": # Compare strings
print("True")
# What to try out
#################
# - Try out how to access the different parts of the string. E.g. how to access
# every 3rd character starting from the second.