Simple notes for Python string features and common string methods, python strings

Source: Internet
Author: User
Tags string methods python list

Simple notes for Python string features and common string methods, python strings

Both single quotation marks and double quotation marks can represent strings. The difference lies in escape.

If you are too lazy to add escape characters, you can add r before the string. For example:

print r'C:\some\name'

Do not wrap a line by adding a backslash to the string.

print """\  Usage: thingy [OPTIONS]  -h      Display this usage message   -H hostname    Hostname to connect to   """

The string is connected by the plus sign and can be doubled by the multiplication sign.
Strings can also be connected by writing them together, but they cannot be used on variables:

'Py' 'thon'

The string can be accessed like an array, and 0 represents the start character. Specifically,-1 indicates the last character, and-2 indicates the last 2nd characters.

The string can be accessed in slices. In particular, a negative number is used for slicing.

S = "abcde" s [0] s [-1] s [-5] s [:-1] # remove the last character, for example, line break + --- + | P | y | t | h | o | n | + --- + 0 1 2 3 4 5 6-6-5-4-3-2-1

When slice access is out of bounds, an empty set is obtained. No need for access control.

A single character cannot be assigned a value because the string is unchangeable. If you need a different string, create a new string and use slice to easily achieve this.
The built-in function len returns the length of the string.

Use encode and decode to ask about string encoding and decoding. (A special file is required to discuss the encoding type issues)

Common Methods:
Startswith detection start
Example:

1. a = 'leonis'
If a. startswith ('le '):
Print 'le'
Endswith detection end
Example:

1. a = ‘leonis'if a.endswith(‘is'):print ‘is'

Whether the in check is a part of it
Example:

1. a = ‘leonis'if ‘o' in a:print ‘a'

Find query contains
Example:

1. a= ‘leonis'if a.find(‘on') != -1:print(‘on')

Join connection string
Example:

1.

 a = ' '
mylist = ['Brazil','Russia','India','China']print a.join(mylist)

2.

mylist = ['Brazil','Russia','India','China']print ‘_'.join(mylist)

Split string

1.

 b = 'my..name..is..leonis'print bprint b.split('..')

2.

b = ‘my..name..is..leonis'print bprint b.split(‘..',1)

Articles you may be interested in:
  • Python's method of determining whether a string is a pure number
  • Summary of N methods for python string connection
  • Python string operations
  • Python Timestamp and time string mutual conversion instance code
  • Split and splice strings in python
  • Python converts a string into a dictionary dict
  • Usage of python string split
  • How to merge connection strings in python list
  • Sort out Python built-in string processing functions
  • Python character string encode and decode research experience garbled Problem Solution
  • Python connection string (join %)

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.