Class python strings
Python Strings
A string is a sequence of characters: 'spam' or "spam"
1. Characters in strings
Use a number in square brackets after the string name to talk about a
character in the string.
Numbering starts at 0. Think of the number as the distance from the
beginning of the string.
Examples:
word = "cabbage"
print(word[0])
print(word[5])
# prints c
# prints g
Odd but useful: The numbering wraps around, so the last character is -1,
the next-to-last is -2, etc.
print(word[-1])
print(word[-2])
# prints e
# prints g
2. A string function: len
len(s) is the number of characters in string s.
len("apple")
len("How are you?")
5
12
3. String operators
in
word1 in word2 is true iff word1 occurs inside word2
Examples:
"a" in "apple"
"a" in "banana"
"a" in "berry"
"berry" in "strawberry"
True
True
False
True
not in
word1 not in word2 is true iff word1 does not occur in word2
Examples:
"a" not in "apple"
"a" not in "berry"
Relational operators: <
>
=
==
!=
The order used is alphabetical order, but all capital letters come before
all lower case letters: "A" < "a", and "Z" < "a".
Examples:
"apple" < "banana"
"apple" < "applesauce"
"Banana" < "apple"
Concatenation: +
Example: "abc" + "def"
abcdef
Concatenating multiple copies: *
Example: 2 * "abc"
abcabc
4. String slices
Assume word is a string. You can take slices (sequences of characters) out of
word by specifying where the slice starts and stops.
Remember that string numbers start at 0.
As with ranges, the stopping value is not included in the list of values.
word = "pineapple"
print(word[0:4]) # pine (characters 0 through 3)
print(word[1:3]) # in (characters 1 through 2)
If you leave off the end of the range, the slice extends to the end of the string:
print(word[4:])
# apple
If you leave off the beginning of the range, the slice begins at the beginning
of the string:
print(word[:4])
# pine (characters 0 through 3)
String slices can be stored in other variables:
fruit = word[4:]
print(fruit)
print(fruit + "sauce")
# apple
# apple
# applesauce
If you want to, you can specify a step size in the slice:
newstring = word[1:5:2]
newstring = word[1:7:2]
newstring = word[1::2]
# ie
# iep
# iepl
5. How string assignment works (string constants are immutable)
You can change what string is stored in a variable:
word = "apple"
word = "banana"
but you can't change the string constant itself:
word = "apple"
word[0] = "A"
# Can't do this
If you want to form a new string, you can make a brand new string from
pieces of the old, using slices, concatenation, etc.
new_word = "A" + word[1:]
or even
word =
"A" + word[1:]
6. String methods
If word , word1, and word2 are strings, here are some methods that you
can use on them:
word.upper()
word.lower()
word1.find(word2)
returns the upper-case version of word
returns the lower-case version of word
returns the index of the beginning of the
first occurrence of word2 in word1
(-1 if not found)
word1.find(word2, start)
same as above, but start the search at start
word1.find(word2, start, stop) same as above, but stop at stop
word.split(delimiter)
separate word into pieces using the specified
delimiter. Returns a list of strings.
................
................
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
- strings lists sets dictionaries and files 4 1 strings
- strings ncert
- strings and pattern matching purdue university
- using python in labeling and field calculations
- lecture 5 strings
- string manipulation based on cbse curriculum class 11
- algorithms linear and binary search
- python strings rxjs ggplot2 python data persistence
- python 3 beginner s reference cheat sheet http www
- introduction to the burrows wheeler transform and fm index
Related searches
- python sort class object
- python get all class attributes
- python get class attributes
- python list class methods
- python get class members
- python join strings together
- python join strings with separator
- python convert list of strings to int
- python change all strings to decimal
- python sort strings with numbers
- using f strings python table
- convert strings to datetime python format