Please enable JavaScript.
Coggle requires JavaScript to display documents.
Strings - Coggle Diagram
Strings
Range slice
(take out several characters of a string)
Pseudocode
someText=”Computer Science”
print(someText.substring(3,3))
Displays:
put
print(someText.left(3))
Displays:
Com
print(someText.right(3))
Displays:
nce
Python
someText=”Computer Science”
print(someText[3:6])
Displays:
put
print(someText[0:7:2])
Displays:
Cmue
(first number in [ ] - starting index)
(Second number in [ ] - ending index + 1)
(Third number in [ ] - step i.e. how many it goes up by)
Traverse
(move through string 1 character at a time)
Pseudocode
string1=”Hello”
//print each character on a separate line
for index = 0 to string1.length -1
print(string1(index))
next index
Displays:
H
e
l
l
o
//Count the number of times a character appears in a string
count=0
for index = 0 to string1.length -1
If string1(index) ==”l” then
count+=1
end if
next index
print(count)
Displays:
2
Python
string1=”Hello”
//print each character on a separate line
for index in range (len(string1)):
print(string1[index])
//Or you can use the “in” string operation
for character in string1:
print(character)
//Count the number of times a character appears in a string
count=0
for index in range (len(string1)):
If string1[index] == “l”:
count+=1
print (count)
//Or you can use a string method
print(string1.count(“l”))
Convert to lower case
Pseudocode
print(someText.lower)
Displays:
computer science
Python
print(someText.lower())
Displays:
computer science
Length
(the number of characters in the string)
Pseudocode
someText=”Computer Science”
print(someText.length)
Displays:
16
Python
someText=”Computer Science”
print(len(someText))
Displays:
16
Slice
(take 1 character out of a string)
Pseudocode
someText=”Computer Science”
print(someText (2))
Displays:
m
Python
someText=”Computer Science”
print(someText [2])
Displays:
m
Convert to uppercase
Pseudocode
someText=”Computer Science”
print(someText.upper)
Displays:
COMPUTER SCIENCE
Python
someText=”Computer Science”
print(someText.upper())
Displays:
COMPUTER SCIENCE
Copy
string1=”hello”
string1=string2
Multiply
It repeats the string as many times as you tell it to :
a=”hello” print(a*2) will give hellohello
Concatenate
(join strings together)
a=”hello” b=”python”
print(a + b) will give
hellopython
String literal
This is when a string is not assigned to a variable : print(“hello”) : hello is a string literal
String variable
This is when a string is assigned to a variable : a=”hello” print(a) output=hello
Index positions
starts at 0 for every character in a string increases by one, negative index positions end at -1