Python String Operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() ![]() ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.
Slice From the Start By leaving out the start index, the range will start at the first character:
Slice To the End By leaving out the end index, the range will go to the end:
Negative Indexing Use negative indexes to start the slice from the end of the string:
Upper Case
Lower Case
Remove Whitespace Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
Replace String
Split String The
String Concatenation To concatenate, or combine, two strings you can use the + operator.
String Format As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:
But we can combine strings and numbers by using the The
The format() method takes unlimited number of arguments, and are placed into the respective placeholders:
You can use index numbers
Escape Character To insert characters that are illegal in a string, use an escape character. An escape character is a backslash An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
Escape CharactersOther escape characters used in Python:
String Methods Python has a set of built-in methods that you can use on strings.
Tags: Python Python Escape Character Python Modify Strings Python Slicing Strings String Concatenation String Format ![]() ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
SIMILAR POSTSPython ListsPython Data TypesString concatenation by join()Printing string n timesPython Arithmetic OperatorsPython Comparison OperatorsPython TuplesPython VariablesPython CommentsPython SetsPython SyntaxPython DictionariesPython Getting StartedPython IntroductionWhat is Python?Python Conditions and If statementsPython While Loops/For LoopsPython FunctionsPython LambdaPython ArraysPython Classes/ObjectsPython InheritancePython IteratorsPython PolymorphismPython ScopePython ModulesCode Chronicles - A Caffeine-Fueled Journey into Data Software EngineeringMachine Learning Types and Programming LanguagesPython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipMastering Excel Data Manipulation with PythonChallenge: One Code Problem Per Day |
OTHER POSTS IN THE SAME CATEGORYPython InheritancePython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython Data TypesPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python? |