Example of string manipulation in Python3

Source: Internet
Author: User
Tags iterable

Example of string manipulation in Python3

#-*-Coding:utf-8-*-
str = ' ABCDEDEDEDEDEFG '

#根据索引获取数据, 0 View first character, 1 view first character starting from right
Print (Str[0])
Print (Str[-1])

#字符串截取/Slice
Print (str[2:]) # CDEDEDEDEDEFG, starting with a character index of 2 to intercept until the end
Print (Str[1:3]) # BC, starting with a character index of 1 and intercepting it to 3-1 positions, not including the 3 position character
Print (Str[:3]) # ABC, truncated from scratch to 3-1 position, without 3 position character
Print (Str[0:6:2]) # Ace, starting with a character index of 0 to intercept to 6-1 position, does not contain 6 position character, slice step is 2, every two characters intercept a
Print (Str[::-1]) # GFEDEDEDEDEDCBA, string flashback output

#使用index获取字符所在索引
Print (Str.index (' C '))

#查找S. Find (sub[, start[, end]) int can search for strings, search within range of start----end slices
Print (Str.find (' F '))
Print (Str.find (' de ', 6,9))

# strip can delete spaces at the beginning and end of a string, and the middle content will not be deleted
f = ' JJ ll '
Print (F.strip ())

#获取字符串的长度
Print (len (str))

#替换S. Replace (old, new[, Count]), str old= modified characters, string new= modified characters, strings, number of times count= replaced
Print (Str.replace (' d ', ' d ')) #返回: ' ABCDEDEDEDEDEFG '
Print (Str.replace (' d ', ' d ', 2)) #返回: ' ABCDEDEDEDEDEFG ' sets the count parameter to only modify two

# S.center (width[, Fillchar]) string-centric, left and right supplementary characters, Width = set widths, fillchar= complement characters
Print (Str.center, '-') #返回: "--ABCDEDEDEDEDEFG---"

Returns True #isnumeric string if it is a pure number
s = ' 123 '
Print (s.isnumeric) #true
Print (Str.isnumeric ()) #Flase

# S.split (Sep=none, maxsplit=-1), List of strings
A = ' Abcebcegbs '
b = A.split (' B ')
Print (b) # returns the list: [' A ', ' CE ', ' ceg ', ' s '], with the character ' B ' as the split symbol, generate the list

#S. Join (iterable)-> str Returns a string that is a string of iterable. The delimiter between elements is S.
Print (' B '. Join (b)) #返回一个字符串 Abcebcegbs

#S. Capitalize ()-> STR Returns an uppercase S, which is the first character, with uppercase letters and other lowercase letters.
Print (Str.capitalize ()) #返回: ABCDEDEDEDEDEFG

Example of string manipulation in Python3

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.