FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 9
PAGE 2 / 2
Strings & Substrings
Home >>> Free Online Lessons, Python 3

Some Useful Functions

The number of functions (methods of the str type) at the moment is [large] and we will only present some that are particularly important for processing strings for you, as beginners.

The associated functions are used as follows:

string.function_name(parameters)

where string is the name of the variable, and function_name is the name of the method.

EXAMPLES

Run the following program:
Editor - lesson_9.py
       
Console/Output done
DETAILS

capitalize() returns a string with the first letter capitalized;

lower() returns a string with all lowercase letters;

upper() returns a string with all uppercase letters;

swapcase() returns a string with all letters swapped from uppercase to lowercase and vice versa;

count(substring[, start, end]) returns the number of occurrences of a substring in the given string; if not found, it returns 0;

find(substring[, start, end]) returns the lowest index where the substring is found in the given string; if not found, it returns -1. The start and end arguments are optional and specify the indices between which to search;

replace(string1, string2, [max_replacements]) returns a new string where all occurrences of string1 are replaced with string2. The optional argument max_replacements allows setting a maximum number of replacements; if not specified, all are considered.

The lesson is now over.
 home   list  CONTENTS   perm_identity   arrow_upward