0% found this document useful (0 votes)
3 views5 pages

Essential String Functions in Python

Uploaded by

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

Essential String Functions in Python

Uploaded by

David Sathurag
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Strings

A string is a sequence:

String ia a sequence of characters you can access the characters one at a time
with bracker operactor or index.

>>>Fruit= ‘banana’
b a n a n a
>>>Letter=Fruit[1]
0 1 2 3 4 5
>>>Print(Letter)

O/P : a

String Functions:

len( ) -> returns the length of the string ie, how many characters present in the
string.

Syn:len(str)

Eg: >>>word= ‘python’

>>> print(“ The length of string is”, len(word))

O/P: The length of string is 6

max( ) -> returns the maximum alaphabetical character from the string.

Syn : max(str)
>>> s= “python”

>>> print(max(s))

O/P: y

min ( ) -> returns minimum alphabetical character from the string.

Syn: min(str)

>>>s= “python”

>>>print(min(s))

count( ):

It is a method count how many times the particular character is present in the
string.

Syn: variable count(“char”)

String Methods:

1) upper ( ) :

Counts lower case letter to upper case letters.

2) lower ( ) :

Counts upper case letter to lower case letters.

Syn:

>>>str= “Hello world”

>>> print ([Link]())


>>> print ([Link]())

O/P: helloworld

HELLO WORLD

3) count( ):

Count how many times the particular character is present in the string.

Syn: [Link](‘character’)

Eg:, >>>str1= “Welcome”

>>>print([Link](‘e”)

O/p: 2

4) find( ):

This method is used to find the location of the particular character in the
string.

Syn: [Link]( ) strfind(“i”,1,2)

1-> start index, 2->end

>>>str= “First year”

>>>print([Link](“i”) O/p: 1

if character not found ->it returns -1

if character found -> return index value

5) join( ): (Join the string with the given character)


This method adds the specified characters in between every character in the
string.

Eg:, str= “hello world”

Print(“,”.join(str))

O/p: h,e,l,l,o,w,o,r,l,d

Used to combine 2 string also.

Eg:

Str1=[“hello”,”world”]

O/p: hello world

Print(“ “.join( str1)

6) string reversed( ):

To reverse the given string used reversed( ) function is used along


with join( )

Eg:,

>>>book=’problem solving’

>>>print(“.join(reversed(book)))

o/P:

7) split( )

Splits a single multiword string in to list of individual works and that are
separated by the given character or spaces.
Eg: str= “Hi How are you”

Print([Link]()) ->[‘Hi’,’How’,’are’,’you’]

Print([Link](“H”)) -> [‘ ‘,’I’,’ow are you]

8)replace( ):

It can taken an original string and return and updated string with some
replacement.

Syn: variable=[Link](“old”,”new”)

Common questions

Powered by AI

The 'upper()' method converts all lowercase letters in a string to uppercase, whereas the 'lower()' method converts all uppercase letters to lowercase. These methods are used to easily change the case of the characters in a string, simplifying case-insensitive comparisons and formatting tasks .

The 'replace()' method facilitates modifications in a string by creating and returning a new string where specified substrings are replaced by new substrings. This method does not modify the original string, reflecting Python strings' immutability principle, meaning the original string remains unchanged, and any transformation necessitates allocation of new memory space for the result .

The 'split()' method divides a string into a list of words based on whitespace by default, effectively breaking the string at spaces and returning a list of substrings. When a specific delimiter is provided, the method splits the string at each occurrence of that delimiter. For example, using 'split("H")' on the string 'Hi How are you' produces a list with elements being the parts of the string around the letter 'H' .

The 'count()' method calculates how many times a specified character appears in the string. For the string 'Welcome', calling the 'count()' method with the argument 'e' would return 2, indicating that the letter 'e' appears twice within the string .

The 'find()' method is used to determine the position of a character within a string by returning the index of the first occurrence of the specified character. If the character is not found, the method returns -1 .

The 'len()' function is critical in string manipulation as it provides the total number of characters in a string, which is essential for iterating operations, slicing, and validating inputs within bounds. For tasks requiring string length knowledge, 'len()' enables logical assessments in conditions and loops where character counting is necessary, such as allocating dynamic arrays or testing string validity .

The 'max()' method identifies and returns the character with the highest alphabetical precedence in a string, while 'min()' identifies the lowest. In the string 'python', 'max()' would return 'y' since it is highest alphabetically, whereas 'min()' would return 'h' since it is the lowest alphabetically among the characters present .

The 'join()' method is used to combine a sequence of strings by inserting a specified separator between each string. For example, the method '.join()' can be used to combine the elements of a list ['hello', 'world'] into a single string 'hello world' using a space as the separator. Alternatively, when invoked as ",".join() on a string 'hello world', it inserts a comma between each character resulting in 'h,e,l,l,o, ,w,o,r,l,d' .

Attempting to print a non-existent index of a string, such as accessing 'banana'[6], results in an IndexError because strings in Python are zero-indexed and attempting to access an index beyond the string's length leads to an out-of-bounds access. This indicates Python's strict enforcement of valid index boundaries in its sequence data types .

The utility of string reversal using the 'reversed()' function along with 'join()' lies in its ability to efficiently generate the reverse of a string in Python, which inherently lacks a built-in reversal method. Utilizing '.join(reversed(string))' allows for concatenating reversed characters into a single output efficiently. Applying it to the string 'problem solving' within the syntax '.join(reversed(book))' produces an inverted output, enabling complex manipulative transformations with minimal costs .

You might also like