Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

String-related operation instructions in Python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "string-related operation instructions in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

(1) slicing operation:

Str1= "hello world!"

Str1 [1:3] 'el' (left closed and right open: i.e. from 1 to 2)

Str [: 3] 'hel'

Str [2:] 'llo worldview'

(2) just like the string in Java, you cannot change the value of the string directly. You can use slicing technique when updating the string:

Str1= "hello world!"

Str1=str1 [: 1] + 'python'+str1 [1:]' hpythonello worldview'

(3) capitalize (): capitalize the first character of a string

> str='hello worldview'

> > str.capitalize ()

'Hello worldview'

> > >

(4) casefold (): lowercase entire string

> str1= "Hello world!"

> > str1.casefold ()

'hello worldview'

> > >

(5) center (width): centers the entire string (supplemented with spaces if not enough width)

Str1= "Hello world!"

> > str1.center (20)

'Hello world!'

> > >

(6) count (sub [, start [, end]]): the number of occurrences of sub from start to end (the default is the entire string)

Str1= "Hello world!"

> str1.count (`lumbjinjia 3)

2 ("Hello world!")

> str1.count ('l')

3 ("Hello world!")

> str1.count

1 ("Hello world!")

> > >

(7) endswith (sub) determines whether it ends with a string.

Str1= "Hello world!"

> str1.endswith ('orldflowers')

True ("Hello world!")

> > >

(8) expandstabs (): converts'\ t'in a string to a space

> str2='include worldview'

> > str2.expandtabs ()

'include worldview'

> > >

(9) find (sub [, start] [, end]): find the location of the substring in the string from start to end and return the subscript

Str1= "Hello world!"

> str1.find ('llo')

2 ("Hello world!")

> str1.find ('llo',3,8)

-1

> > >

(10) isalnum (): determine whether s is a number or letter

Str1= "Hello world!"

> > str1.isalnum ()

False ("Hello world!")

> > >

(11) isspace (): determine whether it is a space or not

> str= ""

> > str.isspace ()

True

> > >

(12) isdigit (): determine whether it is all composed of numbers

> > str= "12345dfgbhn"

> > str.isdigit ()

False ("12345dfgbhn")

> > >

(13) isalpha (): determine whether it is all made up of letters

> str='asdfghj'

> > str.isalpha ()

True

> > >

(14) islower (): determine whether it is all made up of lowercase letters

> str='asdfghj'

> > str.islower ()

True

> > >

(15) istitle (): determines whether it is a title form string (that is, consecutive strings have only the first letter uppercase, the rest are lowercase, and if there are spaces, each delimited string satisfies this)

> str='Helloworld'

> > str.istitle ()

True

> > >

(16) isupper (): determine whether it is all made up of uppercase letters

> str='HELLO WOLD'

> > str.isupper ()

True

> > >

(17) join (sub)

> > str1= "abc"

> str1.join ('1234')

'1abc2abc3abc4'

> > >

(18) lstrip (): remove all spaces on the left side of the string

> str= "hello world!"

> > str.lstrip ()

'hello worldview'

> > >

(19) rstrip (): remove the space on the right side of the string

> str= "hello world!"

> > str.rstrip ()

'hello worldview'

> > >

(20) replace (old, [, new] [, count]): replace the old substring in the string with new, and replace count times

Str='hello worldview'

> str.replace ('hello',' HELLO', 2)

'HELLO world!'

> > >

(21) rfind (sub [, start] [, end]): start from the right to find the location of the string from start to end and return the subscript (note that start and end are from left to right, and the position returned is also from left to right. )

> str= "hello world!"

> str.rfind ('dwindling girls, pamphlet 0, 5)

-1

> str.rfind ('dumped')

ten

> > >

(22) split (sep): splits a string with a given standard and returns the segmented group of elements as a list

>

> str.split (',')

['1mm,' 2pm, '3pm,' 4']

> > >

(23) startwith (sub [, start] [, end]): determine whether to start with sub from start to end

> str.startswith ('hel')

True

> > >

(24) strip (): remove the spaces on the left and right sides of the string

> > str=' hello world!'

> > str.strip ()

'hello worldview'

> > >

(25) swapcase (): reverses the case of a string

> str= "Hello world!"

> > str.swapcase ()

'hELLO Worldwide'

> > >

(26) title () titles the string (that is, the first letter of a consecutive string is uppercase, the rest are lowercase spaces, and all delimited strings follow this rule)

> str= "hello world!"

> > str.title ()

'Hello Worldwide'

> > >

(27) translate (table)

> > str= "sssaabb"

> str.translate (str.maketrans ('s recording dagger'))

'bbbaabb'

> > >

(28) upper (): capitalize the entire string

> str= "hello world!"

> > str.upper ()

'HELLO Worldwide'

> > >

(29) zfill (width): fill in insufficient blanks with'0' (starting from the left)

> str= "hello world!"

> > str.zfill (20)

'00000hello world!'

> > >

(30) lower (): lowercase the entire string

> > str= "HELLO worldQ"

> > str.lower ()

'hello worldq'

> > >

(31) format ()

>'{0} love {1} {2} '.format

I love myhome'

>'{0} love {1} {2} '.format

'I love my home'

>'{a} love {b} {c} '.format.

'I love my home'

>'{0displacement .1f} {1} '.format (27.658 clock GB')

'27.7GB'

> > >

(32) formatting:

> "% d+%d=%d"% (4Jing 5pm 4505)

'4'5'9'

> > >

> >'% c'% 97

'a'

> > >

This is the end of the introduction of "string-related instructions in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report