Summary of python string processing functions
str='python String function'
Generate string variable str='python String function'
Get the string length: len(str)
Example: print '%s length=%d' % (str,len (str))
1. Letter processing
All uppercase: str.upper()
All lowercase: str.lower()
Case swap: str.swapcase()
The first letter is capitalized, the rest is lowercase :str.capitalize()
Capitalize the first letter: str.title()
print '%s lower=%s' % (str,str.lower())
print '%s upper=%s' % (str,str.upper())
print '%s swapcase=%s' % (str,str.swapcase())
print '%s capitalize=%s' % (str,str.capitalize() )
Print '%s title=%s' % (str,str.title())
2. Formatting related
Get a fixed length, right-aligned, and fill in spaces if there is not enough space on the left: str.ljust(width )
Get a fixed length, aligned to the left, and fill in spaces if the right side is not enough: str.ljust(width)
Get a fixed length, aligned in the middle, fill in spaces if there are not enough spaces on both sides: str.ljust(width)
Get a fixed length , right-aligned, fill in the left part with 0s
print '%s ljust=%s' % (str,str.ljust(20))
print '%s rjust=%s' % (str,str.rjust (20))
print '%s center=%s' % (str,str.center(20))
print '%s zfill=%s' % (str,str.zfill(20))
3. String search related
Search for the specified string, no -1 is returned: str.find('t')
Specify the starting position to search: str.find('t',start)
Specify Start and end position search: str.find('t',start,end)
Search from the right: str.rfind('t')
How many specified strings are searched: str.count('t ')
All the above methods can be replaced by index. The difference is that if index is not found, an exception will be thrown, and find returns -1
print '%s find nono=%d' % (str,str.find(' nono'))
print '%s find t=%d' % (str,str.find('t'))
print '%s find t from %d=%d' % (str,1, str.find('t',1))
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
#print '%s index nono ' % (str,str.index('nono',1,2))
print '%s rfind t=%d' % (str,str.rfind('t' ))
Print '%s count t=%d' % (str,str.count('t'))
4. String replacement related
Replace old with new: str.replace(' old','new')
Replace the specified number of times old with new: str.replace('old','new',maxReplaceTimes)
print '%s replace t to *=%s' % (str,str .replace('t', '*'))
print '%s replace t to *=%s' % (str,str.replace('t', '*',1))
5. Remove spaces and specified characters from a string
Remove spaces on both sides: str.strip()
Remove spaces on the left: str.lstrip()
Remove spaces on the right: str.rstrip()
Remove strings on both sides: str.strip('d'), corresponding to lstrip, rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str=' python String function'
print '%s strip=%s' % (str,str.strip('d'))
6. Split the string into an array by specified characters: str.split(' ' )
Default separated by spaces
str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '% s strip=%s' % (str,str.split('-'))
7. String judgment related
Whether it starts with start: str.startswith('start')
Whether it starts with end Ending: str.endswith('end')
Whether it is all letters or numbers: str.isalnum()
Whether it is all letters: str.isalpha()
Whether it is all numbers: str.isdigit()
Whether it is all Lowercase: str.islower()
Whether all uppercase: str.isupper()
str='python String function'
print '%s startwith t=%s' % (str,str.startswith('t' ))
print '%s endwith d=%s' % (str,str.endswith('d'))
print '%s isalnum=%s' % (str,str.isalnum())
str='pythonStringfunction'
print '%s isalnum=%s' % (str,str.isalnum())
print '%s isalpha=%s' % (str,str.isalpha())
print '%s isupper=%s' % (str,str.isupper())
print '%s islower=%s' % (str,str.islower())
print '%s isdigit=%s' % (str,str.isdigit())
str='3423'
print '%s isdigit=%s' % (str,str.isdigit())

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.
