0% found this document useful (0 votes)
6 views3 pages

STRING

In Python, a string is a sequence of characters enclosed in single or double quotes. Strings can be traversed using index values, concatenated with the '+' operator, and replicated with the '*' operator. Various built-in methods and functions are available for string manipulation, such as len(), capitalize(), and split().
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)
6 views3 pages

STRING

In Python, a string is a sequence of characters enclosed in single or double quotes. Strings can be traversed using index values, concatenated with the '+' operator, and replicated with the '*' operator. Various built-in methods and functions are available for string manipulation, such as len(), capitalize(), and split().
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/ 3

STRINGS

What are strings?


In Python, a consecutive sequence of characters , which are enclosed or surrounded
by single (' ') or double (" ") quotes is known as string. This sequence of characters
may include a letter, a number, special character or a backslash.

For example,
Place = "Hathras"

Traversing A String
Traversing a string means accessing all the elements of the string one after other by
using the subscript or index value.

For example,
String = "Information"

I n f o r m a t i o n

0 1 2 3 4 5 6 7 8 9 10

-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

Each character in a string has an index value, indexing starts at 0 and must be an
Integer. Individual elements can be accessed by typing index value inside [ ] square
brackets.

For example,
print (string [2]) = f
Iteration through string
for i in string:
print (I,"end=",") = I,n,f,o,r,m,a,t,i,o,n

Concatenation string ('+' Operator)


Name1="Computer"
Name1="Science"
print(Name1+Name2) = ComputerScience

Replicating strings('*' Operator)


The * operator creates a new string by replicating multiple copies of the name string.
Name = "Computer"
print (Name*3) = ComputerComputerComputer
Membership Operators (in, not in)
'in' operator: It returns true if a character / substring exists in the given string.
'not in' operator: It returns true if character/substring does not exist in the given
string.

Comparison Operators
We can use relational / comparison Operators (>,<,>=,<=,==,!=) to compare two
strings.
Str1 = "Hello"
Str1 = "hello"
print (Str1 == Str2) = False

String slicing
Slicing is used to retrieve a subset of values. A slice of a string is nothing but a
substring.
Syntax:
String_name[start:end]

Note:- String are immutable.

STRING METHODS AND BUILT-IN FUNCTIONS

1. len() - This Method returns the length of the string.

2. capitalise() - This method returns the exact copy of the string with the first letter
in uppercase.

3. split() - The split method breaks up a string at the specified separator and
returns a list substring.

4. isalnum() - Returns True if the characters in the string are alphanumeric.

5. isalpha() - Returns True if all the characters in the string are alphabetic.

6. isdigit() - Returns True if all the characters in the string are digits.

7. isspace() - Returns True if there are only whitespace characters in the string.

8. islower() - Returns True if all cased characters in the string are lowercase.

9. isupper() - Returns True if all cased characters in the string are Uppercase.
10. lower() - Returns a copy of string converted to lowercase.

11. upper() Returns a copy of string converted to uppercase.

12. Istrip() - Returns a copy of string with leading characters removed.

13. rstrip() - Returns a copy of string with trailing characters removed.

You might also like