-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strings.py
73 lines (54 loc) · 1.66 KB
/
Strings.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#-*- coding: utf-8 -*-
#Strings
# String is a type of data in which collections of characters (text) are stored.
# String using "".
a = "Santiago"
b = "Blue"
# Concatenating strings
concat = f"{a} and {b}"
print(concat)
# Counting letters on string
count = len(concat)
print(count)
# Sailing in the index
print(b[0]) #B
print(b[1]) #l
print(b[2]) #u
print(b[3]) #e
# Show string in parts
print(concat[0:8]) #Santiago
print(concat[13:17]) #Blue
print(concat[0:17]) #Santiago and Blue
#In Python, Strings are objects and you can apply methods to them
# Using the method for tinier the words on phrase
a = "CaLiFoRNia"
b = "DReaM"
concat = f"{a} and {b}"
print(concat) # All letters are how initially
print(concat.lower()) # All letters are tiny
print(concat.upper()) # All letters are capital
# Other form
concat = concat.lower() # All letters are tiny, but now is using variable
print(concat)
# For create a line break
concat = f"\n{a}\n and \n{b}"
print(concat)
# For removing special characters and space on the begin and in the end.
print(concat.strip())
# Make string in array and using split
my_string = "2B or not 2B"
my_list = my_string.split(" ")
print(my_list)
# Now removing one letter
my_list = my_string.split("B")
print(my_list)
# Searching substrings
search = my_string.find("not")
print(search) # is return the position on the array
print(my_string[search:]) # return after in the mark on variable
#if search a word what not exist in the context?
search = my_string.find("nothing")
print(search) # is returned -1 position on the array
# and finally, one interesting method is replace
search = my_string.replace("2B", "to be")
print(search) # to be or not to be