0% found this document useful (0 votes)
75 views14 pages

STRINGS

Strings can be created using single, double or triple quotes in Python. Individual characters of a string can be accessed using indexes and slices of characters can be accessed using range slicing. Common string operations include concatenation using +, repetition using *, membership testing using in/not in, and slicing subsets of strings using []. Functions like len(), max(), min() operate on strings. Comparison operators like ==, !=, >, < can compare strings lexicographically.

Uploaded by

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

STRINGS

Strings can be created using single, double or triple quotes in Python. Individual characters of a string can be accessed using indexes and slices of characters can be accessed using range slicing. Common string operations include concatenation using +, repetition using *, membership testing using in/not in, and slicing subsets of strings using []. Functions like len(), max(), min() operate on strings. Comparison operators like ==, !=, >, < can compare strings lexicographically.

Uploaded by

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

UNIT-1

Strings
• String: The collection of elements is known as string. A
string is sequence of characters.
How to create a string in Python?
Strings can be created by enclosing characters inside a single
quote or double quotes. Even triple quotes can be used in
Python but generally used to represent multiline strings.
Example: my_string = “Hello”
print(my_string)
my_string=“””Hello, welcome to the world of
python”””
print(my_string)
Output: Hello
Hello, welcome to the world of python
How to access characters in a string?
• We can access individual characters using indexing and a
range of characters using slicing. Index starts from 0. Trying to
access a character out of index range will raise an IndexError.
The index must be an integer. We can't use float or other
types, this will result into TypeError.

• The index of -1 refers to the last item, -2 to the second last


item and so on. We can access a range of items in a string by
using the slicing operator .
• str = 'programiz'print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])
String operators
• There are many operations that can be performed with string
which makes it one of the most used data type in python.
1. Concatenation “+”
2. Repeatation “*”
3. In
4. Not in
5. Range[n:m]
6. Slice[]
• Concatenation of Two or More Strings
Joining of two or more strings into a single one is called
concatenation.
• The + operator does this in Python. Simply writing two string
literals together also concatenates them.
• The * operator can be used to repeat the string for a given
number of times.
Example: str1 = 'Hello‘
str2 ='World!‘
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
• Output:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
• Membership string:
In and not in : you can use in and not in operators to check
existence of string in another string. They are also known as
membership operator.
S1=“Welcome”
“Come” is in s1
Output: True
S2=“Going”
“wel” is in s1
Output: False
• Not in:
a= “mounika”
“munni” is not in a
Output: True
b=“sneha1”
“sneha” is not in b
Output: False
• Slice[] operator: you can take subset of string
from original string by using[] operator also
known as slicing operator.
in slice[] function elements can be accessing to
the string using index position numbers
Example: a= “welcome”
print(a[2])
Output: l
• Range slice[n:m] operator:
Syntax: s[start:end]
example:
1.S=“welcome”
S[1:3]
Output:el
2.s=“welcome”
S[ :6]
Output: “welcom”
3.s=“welcome”
S[4: ]
Output: “ome”
String functions
• Three are the functions in string:
Function name Function Description
Len() Returns length of the string
Max() Returns character having
highest ASCII value
Min() Returns character having
lowest ASCII value
• Example:
Len(“hello”)=5
Max(“abc”)=“c”
Min(“abc”)= “a”
String Comparison
• You can use(>,<,<=,>=,==,!=) to compare two strings. Python compares
string lexicographically using ASCII value of the characters.
Example:
“tim”== “tie”
False
“free” != “freedom”
True
“arrow” > “aron”
True
“right” >= “left”
True
“teeth” < “tee”
False
“yellow” <= “fellow”
False
“abc” > “ “
True

You might also like