Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

13 - Python - Modify Strings

Python has several built-in string methods for modifying strings including upper() to convert to uppercase, lower() to lowercase, strip() to remove whitespace, replace() to substitute strings, split() to separate by a delimiter into a list, concatenation using +, and format() to insert values into placeholders. Escape characters like \" can be used to include special characters in a string.
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)
27 views

13 - Python - Modify Strings

Python has several built-in string methods for modifying strings including upper() to convert to uppercase, lower() to lowercase, strip() to remove whitespace, replace() to substitute strings, split() to separate by a delimiter into a list, concatenation using +, and format() to insert values into placeholders. Escape characters like \" can be used to include special characters in a string.
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
You are on page 1/ 16

Python - Modify Strings

Upper Case
• Python has a set of built-in methods that you can use on strings.
• The upper() method returns the string in upper case:
Lower Case
• The lower() method returns the string in lower case:
Remove Whitespace
• Whitespace is the space before and/or after the actual text, and very
often you want to remove this space.
• The strip() method removes any whitespace from the beginning or
the end:
Replace String
• The replace() method replaces a string with another string:
Split String
• The split() method returns a list where the text between the specified
separator becomes the list items.
• The split() method splits the string into substrings if it finds instances
of the separator:
String Concatenation
• To concatenate, or combine, two strings you can use the + operator.
• Merge variable a with variable b into variable c:
String Concatenation
• To add a space between them, add a " ":
String Format
• As we learned in the Python Variables chapter, we cannot combine
strings and numbers like this:
String Format
• But we can combine strings and numbers by using the format()
method!
• The format() method takes the passed arguments, formats them, and
places them in the string where the placeholders {} are:
• Use the format() method to insert numbers into strings:
String Format
• The format() method takes unlimited number of arguments, and are
placed into the respective placeholders:
String Format
• You can use index numbers {0} to be sure the arguments are placed in
the correct placeholders:
Escape Characters
• To insert characters that are illegal in a string, use an escape
character.
• An escape character is a backslash \ followed by the character you
want to insert.
• An example of an illegal character is a double quote inside a string
that is surrounded by double quotes:
Escape Characters
• To fix this problem, use the escape character \":
• The escape character allows you to use double quotes when you
normally would not be allowed:
Escape Characters
• Other escape characters used in Python:
Python - String Methods
• Python has a set of built-in methods that you can use on strings.

You might also like