SoFunction
Updated on 2024-11-19

A basic introduction to strings in Python and a summary of common operations

1. Introduction to strings

String format in python:
The variable a, defined as follows, stores a value of numeric type

a = 100

The variable b, defined below, stores a value of type string

b = "hello python"
or
b = 'hello python'

Mini-Summary:
The data in double or single quotes is the string

2. String subscripts

Use of "subscripts" in strings:
Lists and tuples support subscript indexing as well. Strings are actually arrays of characters, so they also support subscript indexing.
If there is the string :name = 'abcdef', the actual storage in memory is as follows.

在这里插入图片描述

Example one:
myname = “dujunyan”
1. Take the first element of the myname string and output it
2. Take the 5th element of the myname string and output it
3. Take the last element of the myname string and output it
4. Take the penultimate element of the myname string and output it

在这里插入图片描述

3. String slicing

Slicing is an operation that intercepts a portion of the object being operated on.
Strings, lists, and tuples all support slicing operations.

Syntax for slicing: [start subscript:end:step]
Note: The selection interval starts from the "Start" bit and ends with the previous bit of the "End" bit (excluding the end bit itself), and the step size indicates the selection interval.
Example two:

在这里插入图片描述

4. String find() operation

Method: find()
Description: Check if str is contained in my_str, if so return the start index, otherwise return -1.
Format: format: my_str.find(str, start=0, end=len(my_str))
Example three:

在这里插入图片描述

5. String index () operation

Method: index()
Method description: Check if str is included in my_str, if so, return the start index value, otherwise report an error.
Format: my_str.index(str, start=0, end=len(my_str))
Example four:

在这里插入图片描述

在这里插入图片描述

6. String count () operation

Method: count()
Method description: Returns the number of times str occurs in my_str between start and end.
Format: my_str.count(str, start=0, end=len(my_str))
Example five:

在这里插入图片描述

7. String replace () operation

Method 04: replace()
Method description: Replace str1 in my_str with str2, if count is specified, then replace no more than count times.
Format: my_str.replace(str1, str2, my_str.count(str1))
Example six:

在这里插入图片描述

8. String split () operation

Method: split()
Method description: slice my_str with str as separator, if maxsplit has a specified value, only maxsplit substrings will be separated.
Format: my_str.split(str=" ", 2)
Example seven:

在这里插入图片描述

9. String startswith() operation

Method: startswith()
method description: check if the string starts with str, if yes, return True, otherwise return False
Format: my_str.startswith(str)
Example eight:

在这里插入图片描述

10. String endswith() operation

Method: endswith()
Method Description: Check if the string ends with obj, if so return True, otherwise return False.
Format: my_str.endswith(obj)
Example nine:

在这里插入图片描述

11. String upper () operation

Method: upper()
Method Description: Convert lowercase letters in my_str to uppercase.
Format: my_str.upper()
Example ten:

在这里插入图片描述

12. String lower () operation

Method 09: lower()
Method Description: Convert all uppercase characters in my_str to lowercase.
Format: my_str.lower()
Example eleven:

在这里插入图片描述

13. String title () operation

Method: title()
Method Description: Capitalize the first letter of each word of the string
Format: my_str.title()
Example twelve:

在这里插入图片描述

14. String capitalize() operation

Method 11: capitalize()
Method description: Capitalize the first character of the string
Format: my_str.capitalize()
Example thirteen:

在这里插入图片描述

15. String strip () operation

Method: strip()
Method Description: Remove whitespace characters from both ends of the my_str string.
Format: my_str.strip()
Example XIV:

在这里插入图片描述

16. String rfind() operation

Method: rfind()
Description: Similar to find(), but from the right.
Format: my_str.rfind(str, start=0,end=len(my_str) )
Example XV:

在这里插入图片描述

17. String join () operation

Method: join()
Description: Insert my_str after each character in str to create a new string.
Format: my_str.join(str)
Example XVI:

在这里插入图片描述

To this point, this article on the basic introduction to the string in Python and a summary of common operations on this article, more related Python string content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!