0% found this document useful (0 votes)
34 views9 pages

38966stringslices

Uploaded by

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

38966stringslices

Uploaded by

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

String slices:

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexes starting at 0
in the beginning of the string and working their way from -1 at the end.

Slice out substrings, sub lists, sub Tuples using index.

Syntax:[Start: stop: steps]

 Slicing will start from index and will go up to stop in step of steps.
 Default value of start is 0,
 Stop is last index of list
 And for step default is 1

For example 1−
str = 'Hello World!'

print str # Prints complete string

print str[0] # Prints first character of the string

print str[2:5] # Prints characters starting from 3rd to 5th

print str[2:] # Prints string starting from 3rd character print

str * 2 # Prints string two times

print str + "TEST" # Prints concatenated string

Output:
Hello World!

llo

llo World!

Hello World!Hello World!

Hello World!TEST

Example 2:
>>> x='computer'
>>> x[1:4]
'omp'
>>> x[1:6:2]
'opt'
>>> x[3:]
'puter'
>>> x[:5]
'compu'
>>> x[-1]
'r'
>>> x[-3:]
'ter'
>>> x[:-2]
'comput'
>>> x[::-2]
'rtpo'
>>> x[::-1]
'retupmoc'

Immutability:
It is tempting to use the [] operator on the left side of an assignment, with the intention of
changing a character in a string.
For example:

>>> greeting='mrcet college!'


>>> greeting[0]='n'

TypeError: 'str' object does not support item assignment

The reason for the error is that strings are immutable, which means we can’t change an
existing string. The best we can do is creating a new string that is a variation on the original:

>>> greeting = 'Hello, world!'


>>> new_greeting = 'J' + greeting[1:]
>>> new_greeting
'Jello, world!'

Note: The plus (+) sign is the string concatenation operator and the asterisk (*) is the
repetition operator
String functions and methods:

There are many methods to operate on String.

S.no Method name Description


1. isalnum() Returns true if string has at least 1 character and all
characters are alphanumeric and false otherwise.
2. isalpha() Returns true if string has at least 1 character and all
characters are alphabetic and false otherwise.
3. isdigit() Returns true if string contains only digits and false
otherwise.
4. islower() Returns true if string has at least 1 cased character and all cased
characters are in lowercase and false
otherwise.
5. isnumeric() Returns true if a string contains only numeric
characters and false otherwise.
6. isspace() Returns true if string contains only whitespace
characters and false otherwise.
7. istitle() Returns true if string is properly “titlecased” and
false otherwise.
8. isupper() Returns true if string has at least one cased character and all
cased characters are in uppercase
and false otherwise.
9. replace(old, new Replaces all occurrences of old in string with new
[, max]) or at most max occurrences if max given.
10. split() Splits string according to delimiter str (space if not
provided) and returns list of substrings;
11. count() Occurrence of a string in another string
12. find() Finding the index of the first occurrence of a string
in another string
13. swapcase() Converts lowercase letters in a string to uppercase
and viceversa
14. startswith(str, Determines if string or a substring of string (if starting index beg
beg=0,end=le and ending index end are given) starts with substring str; returns
n(string)) true if so and false
otherwise.

Note:
All the string methods will be returning either true or false as the result

1. isalnum():
Isalnum() method returns true if string has at least 1 character and all characters are
alphanumeric and false otherwise.

Syntax:
String.isalnum()

Example:
>>> string="123alpha"
>>> string.isalnum() True

2. isalpha():
isalpha() method returns true if string has at least 1 character and all characters are
alphabetic and false otherwise.

Syntax:
String.isalpha()

Example:
>>> string="nikhil"
>>> string.isalpha()
True

3. isdigit():
isdigit() returns true if string contains only digits and false otherwise.

Syntax:
String.isdigit()

Example:
>>> string="123456789"
>>> string.isdigit()
True

4. islower():
Islower() returns true if string has characters that are in lowercase and false otherwise.

Syntax:
String.islower()

Example:
>>> string="nikhil"
>>> string.islower()
True

5. isnumeric():
isnumeric() method returns true if a string contains only numeric characters and false
otherwise.

Syntax:
String.isnumeric()

Example:
>>> string="123456789"
>>> string.isnumeric()
True

6. isspace():
isspace() returns true if string contains only whitespace characters and false otherwise.

Syntax:
String.isspace()

Example:
>>> string=" "
>>> string.isspace()
True

7. istitle()
istitle() method returns true if string is properly “titlecased”(starting letter of each word is
capital) and false otherwise

Syntax:
String.istitle()
Example:
>>> string="Nikhil Is Learning"
>>> string.istitle()
True

8. isupper()
isupper() returns true if string has characters that are in uppercase and false otherwise.

Syntax:
String.isupper()

Example:
>>> string="HELLO"
>>> string.isupper()
True

9. replace()
replace() method replaces all occurrences of old in string with new or at most max
occurrences if max given.

Syntax:
String.replace()

Example:
>>> string="Nikhil Is Learning"
>>> string.replace('Nikhil','Neha')
'Neha Is Learning'

10.split()
split() method splits the string according to delimiter str (space if not provided)

Syntax:
String.split()

Example:
>>> string="Nikhil Is Learning"
>>> string.split()
['Nikhil', 'Is', 'Learning']

11.count()
count() method counts the occurrence of a string in another string Syntax:
String.count()

Example:
>>> string='Nikhil Is Learning'
>>> string.count('i')
3

12.find()
Find() method is used for finding the index of the first occurrence of a string in another
string

Syntax:
String.find(„string‟)

Example:
>>> string="Nikhil Is Learning"
>>> string.find('k')
2
13.swapcase()
converts lowercase letters in a string to uppercase and viceversa

Syntax:
String.find(„string‟)

Example:
>>> string="HELLO"
>>> string.swapcase()
'hello'

14.startswith()
Determines if string or a substring of string (if starting index beg and ending index end are
given) starts with substring str; returns true if so and false otherwise.
Syntax:
String.startswith(„string‟)

Example:
>>> string="Nikhil Is Learning"
>>>
string.starts
with('N')
True

15.endswith()
Determines if string or a substring of string (if starting index beg and ending
index end aregiven) ends with substring str; returns true if so and false otherwise.

Syntax:
String.endswith(„string‟)

Example:
>>> string="Nikhil Is Learning"
>>>
string.start
swith('g')
True

You might also like