0% found this document useful (0 votes)
7 views13 pages

Python String Functions Explained

Uploaded by

iitguru2011
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)
7 views13 pages

Python String Functions Explained

Uploaded by

iitguru2011
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

PYTHON

PROGRAMMIN
G
Which
functions, is it
Events? 
FUNCTIONS
OBJECTIVES

Students will learn about:


• In-built functions and methods in Python
program
▪String is a data type
used to store a
STRING
number of
characters.
For converting cases, the
following functions are used:
▪ [Link]( ): to
convert to uppercase
characters
▪ [Link]( ): to
convert to lowercase characters
Note: These functions work
perfectly in Python.
len( )
▪ It is used to return the length of string.
Ex-
Python program to find the number of characters in your
name .
name=str(input('Enter your name '))
print ('The number of character in your name is ',
len(name))
EXAMPLES-
Python has a set of built-in methods that you can use
on strings.
Note: All string methods returns new values. They do
not change the original string.
Finding the length of a string
mylist = "Hello Everyone"
x = len(mylist)
print(x)

Output: What is the output of the above code? -


Conversion of a string to
lowercase
txt = "Hello my FRIENDS"
x = [Link]( )
print(x)
Output: hello my friends

Conversion of a string to
uppercase
txt = "Hello my FRIENDS"
x = [Link]( )
print(x)
Strip Function
Remove spaces at the beginning and at the end
of the string:
txt = " apple "
x = [Link]()
print(len(txt))
print(len(x))
Output: 9
5
Slicing[Finding substring]​
You can return a range of characters by using the slice
syntax.​
Get the characters from position 2 to position 5 (not
included):​
b = "Hello, World!"​
print(b[2:5])​

Output: 1lo

Slice From the Start​


Get the characters from the start to position 5 (not
included):​
b = "Hello, World!"​
print(b[:5])​
Reverse of a string​
Reverse the string "Hello World":​
txt = "Hello World"
print(txt[::-1] )
Output: dlroW olleH
Extended Study Link:
Sort a string Python String Methods (w3school
Sort a Python String with sorted() [Link])
It results in a list
word = 'datagy'
sorted_word = sorted(word)
print(sorted_word)

Output: ['a','a','d','g','t','y']
random module
Python has a built-in module that you can
use to make random numbers.
The random module has a set of methods:

randint() Returns a random number


between the given range
random() Returns a random float
number between 0 and 1
uniform() Returns a random float Extra Information
number between two given
parameters
Click on the
links for more
information
Python Random randint( )
Definition and Use
The randint( ) method returns an integer number selected element from the specified range.

[Link](start, stop)

Return a number between 10 and 50 (both included):

import random

print([Link](10, 50))
Thank
you

Common questions

Powered by AI

The 'random.randint()' method generates a random integer between specified maximum and minimum values, inclusive. This functionality is important in algorithms that require randomization, such as simulations, randomized testing, or stochastic processes, providing an element of unpredictability and variance that can be crucial for testing and development of robust systems .

Python’s built-in string methods provide essential functionalities for processing and manipulating text data efficiently, such as case conversion, trimming spaces, and finding substring lengths. These methods are beneficial for data cleaning, formatting, and ensuring consistency across applications. However, they might pose limitations when dealing with complex data manipulation or when extensive customization is needed, requiring developers to write additional code or utilize external libraries for more advanced operations .

Both 'upper()' and 'lower()' methods in Python are used for case conversion of strings. The 'upper()' method converts all characters in a string to uppercase, while the 'lower()' method converts them to lowercase. These methods are useful in situations where case-insensitive comparisons are needed or when standardizing data format, such as ensuring all entries in a database column adhere to a consistent case style .

Using sorted() on a string sorts its characters in ascending order from 'a' to 'z', ignoring original character positions, and returns a list of separated characters. This method is useful for tasks that require order normalization or frequency analysis of characters. It does not alter the original string but helps in generating an ordered sequence of its characters .

To reverse a string in Python, the slicing method string[::-1] is used. This technique inverts the order of characters by iterating the string from the end to the start. Reversing strings can be particularly useful in creating palindromic checks, cryptographic applications, or simply generating data presentations that require inverted sequences .

The 'random()' function generates a random float number between 0.0 and 1.0. This is particularly useful in simulations and probabilistic modeling where random sampling from a uniform distribution is needed. Applications include Monte Carlo simulations, randomized algorithms, and any scenario where normalized random data is required for experimental setups or testing frameworks .

The 'random.uniform()' method generates a pseudo-random floating-point number between two specified values, allowing for more precise and granular random number generation compared to 'random.randint()', which outputs only integer values. This fine-tuned control is ideal for calculations where decimal precision is necessary, such as simulations that model real-world phenomena .

The 'len()' function in Python calculates and returns the number of characters present in a string, including spaces and special characters. This function is crucial for data validation, such as checking password length requirements or validating input fields, and in operations that require knowledge of string size, like slicing or iterating through characters .

The 'strip()' method is used to clean data by removing any leading and trailing whitespace from a string. This is particularly useful in data preprocessing stages where extraneous spaces can lead to incorrect data interpretations or corrupt data alignments. By eliminating these spaces, 'strip()' ensures the consistency and accuracy of data input and processing .

String slicing in Python is used to extract a portion of a string, known as a substring, using a defined range of characters. It is performed with the syntax string[start:stop], where 'start' is the beginning index and 'stop' is the ending index (not included). When only a starting index is provided, such as string[start:], the slice includes all characters from the start to the end of the string. Conversely, if only an ending index is specified, like string[:stop], the slice includes characters from the beginning of the string up to, but not including, the stop index.

You might also like