Introduction - Home | CS Dept
2/21/22
CS 224 Introduction to Python
Strings
1
Python Strings
Python strings are immuatable:
s = ¡®abc¡¯
s[2] = ¡®d¡¯
s = ¡®abd¡¯
s = s[:-1] + ¡®d¡¯
These don¡¯t change the string ¡®abc¡¯
they reassign the string variable s
2
1
2/21/22
Accessor and Slices
s = ¡¯A diamond necklace played the pawn¡¯
print(s[5])
print(s[len(s)-1]
print(s[-1])
print(s[-2])
print(s[10:18])
t = s[19:]
u = s[:9]
print(s[-4:])
v = s[:-16]
#
#
#
#
#
#
#
#
#
prints m
prints n
prints n
prints w
prints necklace
t is ¡®played the pawn¡¯
u is ¡®A diamond¡¯
prints pawn
v is ¡®A diamond necklace¡¯
3
Methods
Python provides a broad array of string operations.
Because strings are immuatable, the operations do not modify the string.
Most methods fall into one of the following categories:
? Return a new string
? Return a Boolean
? Return an int
? Return a tuple or list
4
2
2/21/22
Methods that return a string
?
?
?
?
?
?
?
s.capitalize()
s.lower()
s.upper()
s.join(t)
s.replace(old, new)
s.strip()
s.rstrip()
5
Upper and lower case
s.lower()
returns an all lower case conversion of s
s.upper()
returns an all upper case conversion of s
s.capitalize()
returns s with first character capitalized
6
3
2/21/22
Examples: upper and lower case
s = ¡®Hand in hand some drummed along¡¯
s.lower()
returns ¡®hand in hand some drummed along¡¯
s.upper()
returns ¡®HAND IN HAND SOME DRUMMED ALONG¡¯
s.lower().capitalize()
returns ¡®Hand in hand some drummed along¡¯
¡®123¡¯.capitalize()
returns ¡®123¡¯
7
Join
s.join(t)
t is an iterable of strings
returns a concatenation of strings in t with
string s as a separator
8
4
2/21/22
Examples: join
s = ¡® ¡¯
t = [¡®child¡¯, ¡®is¡¯, ¡®the¡¯, ¡®father¡¯]
s.join(t)
# t is an iterable of strings
returns ¡®child is the father¡¯
¡®,¡®.join(t)
returns ¡®child,is,the,father¡¯
¡® la ¡®.join(t)
returns ¡®child la is la the la father¡¯
9
Replace
s.replace(old, new)
old is a string
new is a string
returns a string in which occurrences of old
have been replaced with new
10
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- information visualization color
- building data science applications in tableau
- package viridis
- ch 10 color
- somewhere over the rainbow an empirical assessment of
- fenics course
- introduction to data visualization color mapping
- cs 111 program design i
- introduction home cs dept
- generating colors from the viridis color scale with a sas
Related searches
- cs ny employee benefits nyship
- 7 cs of communication ppt
- cs ny gov employee benefits
- 7 cs of effective communication
- the 7 cs of communication
- cs phd salary
- seven cs of communication
- project ideas for cs students
- dept of agriculture home loan
- oxford cs philosophy
- the 7 cs of communication definitions
- freud and cs lewis