0% found this document useful (0 votes)
90 views12 pages

Strings

Strings are sequences of characters that can be defined with single or double quotes in Python. Each string is stored in memory as a list of characters that can be accessed using indices. Common string operations include concatenation, indexing substrings, finding length and locating substrings. String methods like upper(), lower(), count(), replace(), and find() return new strings without modifying the original. Strings are immutable, so assigning to an index will fail and methods do not change the original string.

Uploaded by

ria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views12 pages

Strings

Strings are sequences of characters that can be defined with single or double quotes in Python. Each string is stored in memory as a list of characters that can be accessed using indices. Common string operations include concatenation, indexing substrings, finding length and locating substrings. String methods like upper(), lower(), count(), replace(), and find() return new strings without modifying the original. Strings are immutable, so assigning to an index will fail and methods do not change the original string.

Uploaded by

ria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Strings

Strings
• A string is a sequence of letters (called
characters).
• In Python, strings start and end with single
or double quotes.
>>> “foo”
‘foo’
>>> ‘foo’
‘foo’
Defining strings
• Each string is stored in the computer’s
memory as a list of characters.
>>> myString = “GATTACA”

myString
Accessing single characters
• You can access individual characters by using indices in square brackets.

>>> myString = “GATTACA”


>>> myString[0]
‘G’
>>> myString[1]
‘A’
>>> myString[-1]
‘A’ Negative indices start at the end
>>> myString[-2] of the string and move left.
‘C’
>>> myString[7]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: string index out of range
Accessing substrings
>>> myString = “GATTACA”
>>> myString[1:3]
‘AT’
>>> myString[:3]
‘GAT’
>>> myString[4:]
‘ACA’
>>> myString[3:5]
‘TA’
>>> myString[:]
‘GATTACA’
Special characters
• The backslash is used to
introduce a special character.
Escape Meaning
sequence
>>> "He said, "Wow!""
\\ Backslash
File "<stdin>", line 1
"He said, "Wow!""
^ \’ Single quote
SyntaxError: invalid
syntax \” Double
>>> "He said, 'Wow!'"
quote
"He said, 'Wow!'"
>>> "He said, \"Wow!\"" \n Newline
'He said, "Wow!"'
\t Tab
More string functionality
>>> len(“GATTACA”) ← Length
7
>>> “GAT” + “TACA”
‘GATTACA’ ← Concatenation
>>> “A” * 10
‘AAAAAAAAAA ← Repeat
>>> “GAT” in “GATTACA”
True
← Substring test
>>> “AGT” in “GATTACA”
False
String methods
• In Python, a method is a function that is
defined with respect to a particular object.
• The syntax is
<object>.<method>(<parameters>)

>>> dna = “ACGT”


>>> dna.find(“T”)
3
String methods
>>> "GATTACA".find("ATT")
1
>>> "GATTACA".count("T")
2
>>> "GATTACA".lower()
'gattaca'
>>> "gattaca".upper()
'GATTACA'
>>> "GATTACA".replace("G", "U")
'UATTACA‘
>>> "GATTACA".replace("C", "U")
'GATTAUA'
>>> "GATTACA".replace("AT", "**")
'G**TACA'
>>> "GATTACA".startswith("G")
True
>>> "GATTACA".startswith("g")
False
Strings are immutable
• Strings cannot be modified; instead, create a
new one.

>>> s = "GATTACA"
>>> s[3] = "C"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> s = s[:3] + "C" + s[4:]
>>> s
'GATCACA'
>>> s = s.replace("G","U")
>>> s
'UATCACA'
Strings are immutable
• String methods do not modify the string;
they return a new string.
>>> sequence = “ACGT”
>>> sequence.replace(“A”, “G”)
‘GCGT’
>>> print sequence
ACGT

>>> sequence = “ACGT”


>>> new_sequence = sequence.replace(“A”, “G”)
>>> print new_sequence
GCGT
String summary
Basic string operations:
S = "AATTGG" # assignment - or use single quotes ' '
s1 + s2 # concatenate
s2 * 3 # repeat string
s2[i] # index character at position 'i'
s2[x:y] # index a substring
len(S) # get length of string
int(S) # or use float(S) # turn a string into an integer or floating point decimal

Methods:
S.upper()
S.lower()
S.count(substring)
S.replace(old,new)
S.find(substring)
S.startswith(substring), S. endswith(substring)

Printing:
print var1,var2,var3 # print multiple variables
print "text",var1,"text" # print a combination of explicit text (strings) and variables

You might also like