3.2.8 - String handling operations in a programming language Flashcards
(7 cards)
length
Determines the number of characters in a string, including spaces and punctuation.
len(“Hello”) - # returns 5
Position
Finds the index of a specified character or substring within a larger string.
text = “computer”
position = text.find(“p”)
print(position) - # returns “3”
Substring
Extracts a portion of a string, defined by a starting position and length or end position.
“computer”[1:4] - # returns “omp”
Concatenation
Joining two or more strings end-to-end to form a new string.
“Hello “ + “World” - # returns “Hello World”
Convert character to character code
Returns the numerical code (e.g., ASCII) for a character.
ord(‘A’) # returns 65
Convert character code to character
Returns the character represented by a character code.
chr(66) # returns ‘B’
String conversion operations
int(“42”) # string to integer → 42
float(“3.14”) # string to real → 3.14
str(99) # integer to string → “99”
str(2.5) # real to string → “2.5”