1
NADAR SARASWATHI COLLEGE OF ARTS&SCIENCE,THENI
DEPARTMENT OF COMPUTER SCIENCE&INFORMATION TECHNOLOGY
M JEYAVARTHINI
I MSC(CS)
2
TOPIC: STRING
STRING
String:
• Strings in python are surrounded by either
single quotation marks, or double quotation
marks.
• 'hello' is the same as "hello".
• print() function:
Example:
print("Hello")
print('Hello') 3
Assign String to a Variable:
• Assigning a string to a variable is done with the
variable name followed by an equal sign and the
string:
Example :
a = "Hello"
print(a)
4
Multiline Strings:
• You can assign a multiline string to a variable by
using three quotes:
Example:
You can use three double quotes:
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a) 5
Example2:
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
6
7
Strings are Arrays:
• Like many other popular programming languages,
strings in Python are arrays of bytes representing
unicode characters.
• Python does not have a character data type, a single
character is simply a string with a length of 1.
• Square brackets can be used to access elements of the
string.
Example:
Get the character at position 1 (remember that the first
character has the position 0):
a = "Hello, World!"
print(a[1])
Looping Through a String:
• Since strings are arrays, we can loop through
the characters in a string, with a for loop.
Example:
Loop through the letters in the word "banana":
for x in "banana":
print(x).
8
String Length:
To get the length of a string, use the len() function.
Example:
The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))
9
Check String:
• To check if a certain phrase or character is present in a string,
we can use the keyword in.
Example:
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Use it in an if statement:
Example:
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
10
Check if NOT:
To check if a certain phrase or character is NOT present in a string,
we can use the keyword not in.
Example:
Check if "expensive" is NOT present in the following text:
txt = "The best things in life are free!"
print("expensive" not in txt)
Use it in an if statement:
Example:
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
11
12
String Methods:
• The most common string methods. A method is like a
function, but it runs "on" an object. If the variable s is a
string, then the code s.lower() runs the lower() method on
that string object and returns the result (this idea of a
method running on an object is one of the basic ideas that
make up Object Oriented Programming, OOP). Here are some
of the most common string methods:
• s.lower(), s.upper() -- returns the lowercase or uppercase
version of the string
• s.strip() -- returns a string with whitespace removed from the
start and end
• s.isalpha()/s.isdigit()/s.isspace()... -- tests if all the string chars
are in the various character classes 13
• s.find('other') -- searches for the given other string (not a
regular expression) within s, and returns the first index where
it begins or -1 if not found
• s.replace('old', 'new') -- returns a string where all occurrences
of 'old' have been replaced by 'new'
• s.split('delim') -- returns a list of substrings separated by the
given delimiter. The delimiter is not a regular expression, it's
just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a
convenient special case s.split() (with no arguments) splits on
all whitespace chars.
14
• s.join(list) -- opposite of split(), joins the elements in the
given list together using the string as the delimiter. e.g. '---
'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc
1. A google search for "python str" should lead you to the
official python.org string methods which lists all the str
methods.
2. Python does not have a separate character type. Instead an
expression like s[8] returns a string-length-1 containing the
character. With that string-length-1, the operators ==, <=, ...
all work as you would expect, so mostly you don't need to
know that Python does not have a separate scalar "char"
type.
15
16
17

varthini python .pptx

  • 1.
    1 NADAR SARASWATHI COLLEGEOF ARTS&SCIENCE,THENI DEPARTMENT OF COMPUTER SCIENCE&INFORMATION TECHNOLOGY M JEYAVARTHINI I MSC(CS)
  • 2.
  • 3.
    STRING String: • Strings inpython are surrounded by either single quotation marks, or double quotation marks. • 'hello' is the same as "hello". • print() function: Example: print("Hello") print('Hello') 3
  • 4.
    Assign String toa Variable: • Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example : a = "Hello" print(a) 4
  • 5.
    Multiline Strings: • Youcan assign a multiline string to a variable by using three quotes: Example: You can use three double quotes: a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""" print(a) 5
  • 6.
    Example2: a = '''Loremipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''' print(a) 6
  • 7.
    7 Strings are Arrays: •Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. • Python does not have a character data type, a single character is simply a string with a length of 1. • Square brackets can be used to access elements of the string. Example: Get the character at position 1 (remember that the first character has the position 0): a = "Hello, World!" print(a[1])
  • 8.
    Looping Through aString: • Since strings are arrays, we can loop through the characters in a string, with a for loop. Example: Loop through the letters in the word "banana": for x in "banana": print(x). 8
  • 9.
    String Length: To getthe length of a string, use the len() function. Example: The len() function returns the length of a string: a = "Hello, World!" print(len(a)) 9
  • 10.
    Check String: • Tocheck if a certain phrase or character is present in a string, we can use the keyword in. Example: Check if "free" is present in the following text: txt = "The best things in life are free!" print("free" in txt) Use it in an if statement: Example: Print only if "free" is present: txt = "The best things in life are free!" if "free" in txt: print("Yes, 'free' is present.") 10
  • 11.
    Check if NOT: Tocheck if a certain phrase or character is NOT present in a string, we can use the keyword not in. Example: Check if "expensive" is NOT present in the following text: txt = "The best things in life are free!" print("expensive" not in txt) Use it in an if statement: Example: print only if "expensive" is NOT present: txt = "The best things in life are free!" if "expensive" not in txt: print("No, 'expensive' is NOT present.") 11
  • 12.
  • 13.
    String Methods: • Themost common string methods. A method is like a function, but it runs "on" an object. If the variable s is a string, then the code s.lower() runs the lower() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, OOP). Here are some of the most common string methods: • s.lower(), s.upper() -- returns the lowercase or uppercase version of the string • s.strip() -- returns a string with whitespace removed from the start and end • s.isalpha()/s.isdigit()/s.isspace()... -- tests if all the string chars are in the various character classes 13
  • 14.
    • s.find('other') --searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found • s.replace('old', 'new') -- returns a string where all occurrences of 'old' have been replaced by 'new' • s.split('delim') -- returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all whitespace chars. 14
  • 15.
    • s.join(list) --opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. '--- '.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc 1. A google search for "python str" should lead you to the official python.org string methods which lists all the str methods. 2. Python does not have a separate character type. Instead an expression like s[8] returns a string-length-1 containing the character. With that string-length-1, the operators ==, <=, ... all work as you would expect, so mostly you don't need to know that Python does not have a separate scalar "char" type. 15
  • 16.
  • 17.