You are on page 1of 3

Week 5 Unit 2 (Python Strings)

STRING CONCATENATION
 In here, we will add one string to the end of another.
 This concept is just one of many ways to add string variables
together to complete a larger string.
 using the addition operator without variables:

PYTHON STRINGS sample code:


name = "John" + " " + "Smith"
print(name)

 using the addition operator with variables:


sample code:
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)

47 48 By: JMI

Formatting STRING using “ f ” Formatting STRING using “ % ” Operator

 This formatting method is applicable to python 3.6 to present  This formatting method use percent operators (%) to mark the location of
 The new way to insert variables into a string in Python is by using what we the variable being inserted.
call f strings.  The letter after the percent operator signifies data type.
 By putting the letter “f” in front of a string, you’re able to insert a variable  Use %s to insert strings and %d for digit
into a string directly in line.  After the string closes, you would place a percent operator, followed by the
 To insert a variable in a string, simply wrap curly brackets around the name variables you would like to use.
of the variable.  using the % operator for single  using the % operator for single number
 using the new f strings string variables formatting variables formatting
sample code:
name = "John" sample code: sample code:
print( f"Hello {name}" ) name = 'Jeanne' age = 35
print('Hello, %s' % name) print('John is %d years old. ' % age)

49 By: JMI 50 By: JMI

ECE 106 - Computer Programming


Prepared by: Engr. Jeanne M. Imbuido 1
Week 5 Unit 2 (Python Strings)

Formatting STRING using “ % ” Operator STRING INDEX

 using the % operator for multiple string variables formatting  When a computer saves a string into memory, each character within the string is
assigned what we call an “index.” An index is essentially a location in memory.
sample code:
first_name = "Jeanne"  For example, we take a string like “Hello” and break down their indexes, we can see
last_name = " Imbuido" that the letter “H” is located at index zero, refer to sample code:
print( "Hello, %s %s." % (first_name, last_name) )  using # using indexes to print each element
# Enclose the variables in parenthesis sample code:
word = "Hello"
 using the % operator for multiple number variables formatting print( word[ 0 ] ) # will output 'H'
print( word[ 1 ] ) # will output 'e'
sample code: print( word[ -1 ] ) # will output 'o'
b =9  In order to index a specific element, you use square brackets to the right of the
l=7 variable name. Within those square brackets, you put the index location you wish to
p=2 access.
a= " Saint Agatha Homes, Tikay, Malolos City "
print(‘ I am currently residing at Blk.%d Lot %d Phase %d %s. ' % (b, l, p, a) )  Using negative index numbers will result in trying to access information from the
back, such that -4 would result in the output of the letter “e”.
51 By: JMI 52 By: JMI

STRING INDEX STRING SLICING

 using # using indexes to print each  Slicing is used mostly with Python lists, however, you can use it on strings as well.
element  String slicing is essentially when you only want a piece of the variable.

sample code:
for example, if we only wanted “He” from the word “Hello”,
word = “Jeanne"
print( word[ 0 ] ) # will output ‘J'
print( word[ 1 ] ) # will output 'e‘  Will output “He” only:
print( word[ 2 ] ) # will output ‘a'
print( word[ 3 ] ) # will output ‘n' sample code:
print( word[ 4 ] ) # will output ‘n‘ print( word[ 0 : 2 ] ) # will output 'He'
print( word[ 5 ] ) # will output 'e‘ print( word[ 1 : 4 ] ) # will output ‘ell'
print( word[ 6 ] ) # will crashed your
program
 The first number in the bracket is the starting index; the second is the stopping
index (Note: this always starts counting from the beginning letter.
53 By: JMI 54 By: JMI

ECE 106 - Computer Programming


Prepared by: Engr. Jeanne M. Imbuido 2
Week 5 Unit 2 (Python Strings)

STRING Manipulation STRING Manipulation


 using the find method “.find()” to search for any string :
 String manipulation just means that we want to alter what the current string is.
 using the title method “.title()” to capitalize the beginning of a string: sample code: finding the starting index of our searched word
sample code: s = "Look over that way"
name = "jOhn sMith“ print( s.find("over") )
print( name.title( ) ) #capitalize the beginning of the string
print( name.lower( ) ) # all the string in lower case  Find returns the starting index position of the match; If you count where the word “over”
print( name.upper( ) ) #capitalize all the string begins, the “o” is at index location 5.
 using the strip method “.strip()” to get rid of a certain character on the left and right side
 using the replace method “.replace()” to find and replace values
of a string
sample code: replacing an exclamation point (!) with a periods (…)
sample code: removing white space with strip
words = "Hello there!"
name = " john "
print( words.replace( "!", "…" ) )
print( name.strip( ) )

#Try .lstrip( ) and .rstrip( ) and see what happens


55 By: JMI 56 By: JMI

ECE 106 - Computer Programming


Prepared by: Engr. Jeanne M. Imbuido 3

You might also like