Python character processing

Source: Internet
Author: User

A string is a series of characters. In Python, a string is enclosed in quotation marks, where the quotation marks can be single quotes or double quotes.

For example:

>>> ' This is a string '
' This is a string '
>>> "This is also a string"
"This is also a string"

Operations on Strings:

#修改字符串的大小写 #

1. Modify the first character of the word

>>> name= ' Alben xue '
>>> type (name)
<class ' str ' >
>>> print (Name.title ())
Alben Xue

The title () follows the variable, using the. Connection, representing the variable name in the method defined by the title ()

2, modify all characters

>>> Name
' Alben Xue '
>>> print (Name.upper ())
Alben XUE
>>> print (Name.lower ())
Alben Xue

#合并字符串 #

In Python, string merging with "+"

Example:

>>> first_name= ' Xue '
>>> last_name= ' lingming '
>>> Full_name=first_name+last_name
>>> message= ' Hello, ' +full_name.upper () + '! '
>>> Print (message)
hello,xuelingming!

"The code above, storing a string of strings in a variable, all subsequent operations on the variable execution, this will be more convenient"

#使用制表符或换行符来添加空白 #

In programming, whitespace refers to any nonprinting character, such as spaces, tabs, newline characters.

1. Using tabs

>>> print (' Python ')
Python
>>> print (' \tpython ')
Python

2. Use newline characters

>>> print (' Python\c##\nphp\njavascript ')
python\c##
Php
Javascript

3. Tabs and newline characters used together

>>> print (' languages:\n\tpython\n\tc##\n\tphp ')
Languages:
Python
c##
Php

#删除多余的空白符 #

>>> favorite_language= ' python '
>>> Favorite_language
' Python '

You can see that there is a space in this variable, which is recognizable to the program, superfluous to a person, and how to remove

>>> Favorite_language.rstrip ()
' Python '

This is only temporary, in order to permanently delete the blank, you need to store this string in a variable

>>> Favorite_language=favorite_language.rstrip ()
>>> Favorite_language
' Python '

Culling characters at the beginning of a string

>>> name= ' Alben '
>>> Name
' Alben '
>>> Name.lstrip () #去头 #
' Alben '
>>> Name.rstrip () #去尾 #
' Alben '
>>> Name.strip () #去头去尾 #
' Alben '

P

Python character processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.