Programming in Python. Working with strings

Date:

2018-03-19 05:25:24

Views:

663

Rating:

1Like 0Dislike

Share:

Table of contents:

In the daily life of the programmer need to solve a lot of problems. For these purposes, perfect Python language (“online” or “Python” on Russian manners). Backup, creating games for Android or for Windows, read and save messages from e-mail – not the most difficult task for an experienced programmer. But for beginners to learn Python (work with strings, lists, files,) will seem like a fairy tale, because the methods and functions of each type of data make life easier for the user.

python job

What is string data type?

“Python” allows you to work with numbers, symbols, files, functions. And difficulties never occurs because this language is convenient and easy to use. Strings in Python – the type of data to process textual information. Length the entered text can be limited only by the computer resources. You need to remember that line – immutable data type. Everything that happens with the original text is assigned to a new variable.

In Python training involves the study of 2 kinds of text: ordinary strings (sequence of bytes) and Unicode string (list of characters).

In Python scripts allow you to use string literals. These symbols can be written using the apostrophe, quotation marks (single or double), the specified characters is entered three times, for example, """apple tree""" where performs the role of the Enter key when typing in a regular text editor. The backslash (back slash) inside of the literals is of special significance. It is necessary to enter special characters. Remember that the backslash must not be last character in the string, otherwise the compiler will issue an error.

Recommended

How to get out of

How to get out of "Skype" on the "Android" and not only

How to get out of "Skype" "Android"? This question bothers many users. The thing is that the messenger is not so difficult. But there are features that are only for mobile versions. To log out of Skype in this case more difficult than it seems. But i...

Kingo ROOT: how to use the program to gain administrative rights on Android

Kingo ROOT: how to use the program to gain administrative rights on Android

The Gadgets on the platform Android regained the lion's share of the market. Developers are continually perfecting the OS, trying to fit the needs of the users, but from year to year admit palpable mistake: forbid the owner to "stuffing" of his gadge...

How to get from Stormwind to Tanaris: practical tips

How to get from Stormwind to Tanaris: practical tips

World of Warcraft is an iconic MMORPG, uniting millions of players around the world. There are many locations and puzzles, secret trails and larger, but rather dangerous paths. Sooner or later, every player chose the side of the Alliance, we have to ...

How to create a string?

In Python, working with strings involves either manual entry of the text or prompt to the user that he entered the necessary characters. Python reads the received data using the standard input function (). String in Python consists of a sequence of characters. To know their number, you can use len (). In Python, the symbol is treated as a string, the length of which is equal to 1.

Any object in the “Python” can be cast to string data type by calling a built-in function str (). Python has several classes. To translate an object from one data type to another, you can use the cast function whose name matches the name of the category. So, int converts to an integer, str the string float in the float category.

Sections in the rows

Otherwise, they are referred to as slices or slices. Slice allows you to extract from the typed character in a specified period. Slice cannot be used without indexing. Addressing starts with 0. If you refer to the line from the end, then the indexing begins with -.

String ST

W

O

R

L

D

Indexing

ST[0]

ST[1]

ST[2]

ST[3]

ST[4]

Indexing

ST[-5]

ST[-4]

ST[-3]

ST[-2]

ST[-1]

python training

There are 3 forms of cuts:

  • Extract a single character from whole string St[i], where St – the text i – cell number.
  • Get text, St[a:b], where St – the text, a and b – the beginning and end of period. A is included in the slice, the parameter b is not. If in the interval you do not specify b, and put a colon, then the program will return an excerpt, taken to end of line. If you do not set the parameter a, the slice originates from a 0 index to the end of paragraph b.
  • Cut St[a:b:d] allows you to select a specific passage in the interval beginning at a and ending at b with step d.

Operations with strings

The user has the ability to use Python functions to work with strings:

  • Str(X) – translation of any object in a string data type.
  • ST1+ST2 – concatenation (addition of strings).
  • ST*n-ST repeat string n times.
  • Min [ST] – returns minimum value from a code table.
  • Len () – determining the length of the text.
  • Max [ST] – obtaining maximum values from the ASCII table.
  • St1 in St2 – occurrence of a substring St1 to St2. Returns True if St1 is present in the original text.
  • Not in St1, St2 – checks that the St1 to St2 and returns True when the correctness of the statements.

python character

Methods of string data type

In Python, working with strings involves the use of several methods:

  • St. find and St.rfind – the methods necessary to search for a passage in the entered text. The difference is that this process will start from different ends. So, St.find searches from the beginning, and St.rfind from the end. The methods return the index of the first occurrence of the searched text. You can specify the sections to search in a particular period.
  • St. replace (a_old,b_new) needed to replace all occurrences of the substrings in the entered text. Instead a_old will be b_new. If you specify an additional count parameter (St.replace (a_old,b_new, count)), then the change will occur the number of times is not greater than count.
  • St. count – the method required for counting the number of occurrences of substring S in the input text. The number of intersections is not included in the final result. You can specify the length, which will be the operation.
  • St. join for concatenating the list of words in a string.
  • St. split – a method to turn the string into a list of words. In brackets in quotes to specify the delimiter. Most often it is the space St. split(“ ”).
  • St. strip – remove spaces at the beginning and end of the string.
  • St. capitalize makes the first character in the text uppercase.
  • St. swapcase allows you to convert the case of letters in the opposite direction.
  • St. upper gives the opportunity to change all letters to uppercase in the string.
  • St. lower allows you to transfer the entered text to lower case. All letters will be lowercase.

python scripts

Formatted string

In Python, working with strings is nice and the fact that the user can format the text in the specified image. I.e. the programmer specifies the condition which needs to match the entered text. This way you can display the table without connecting a special module PTable. This technique is called a format string. For example, if you enter in IDLE Print "%s is %d" % ("one",1), then the result will be 1 is one.

the% allows you to connect the format string and is its main operator. In Python characters are coming as addition to %.

Format

Clarification

%

Output a single character.

%s

A String expression.

%d

Decimal integer.

%f

Output real (fractional) decimal number.

python functions

There are other special characters, but they are used in solving complex tasks.

Multiple problems with solutions

A language like Python allows learning to be faster and easier because the code is concise and comprehensive. To verify this, you need to address a number of problems.

1. Given a string. Get a new text, which replaces all occurrences of the first symbol of the “$” except for himself.

Algorithm:

  • Write a function, which:
    • A char variable is assigned the first character string;
    • Length specifies the length of the string;
    • Str1 using the replace method replaces the char with the symbol "$";
    • Str1 writes the first character and the resulting expressions from the previous operation;
    • Return the value of str1.
  • Display the result of the calculation function.

python job

2. Dana...


Article in other languages:

AR: https://tostpost.com/ar/computers/4642-programming-in-python-working-with-strings.html

BE: https://tostpost.com/be/kamputary/8250-pragramavanne-python-praca-z-radkam.html

DE: https://tostpost.com/de/computer/8252-programmierung-in-python-arbeiten-mit-strings.html

ES: https://tostpost.com/es/los-ordenadores/8257-la-programaci-n-en-python-trabajar-con-l-neas.html

HI: https://tostpost.com/hi/computers/4645-programming-in-python-working-with-strings.html

JA: https://tostpost.com/ja/computers/4643-programming-in-python-working-with-strings.html

KK: https://tostpost.com/kk/komp-yuterler/8255-ba-darlamalau-python-zholdarmen-zh-mys.html

PL: https://tostpost.com/pl/komputery/8254-programowanie-w-j-zyku-python-praca-z-wierszami.html

PT: https://tostpost.com/pt/computadores/8248-programa-o-em-python-trabalhar-com-linhas-de.html

TR: https://tostpost.com/tr/bilgisayarlar/8259-programlama-python-dizelerle-al-ma.html

UK: https://tostpost.com/uk/komp-yuteri/8255-programuvannya-python-robota-z-ryadkami.html

ZH: https://tostpost.com/zh/computers/5038-programming-in-python-working-with-strings.html






Alin Trodden - author of the article, editor
"Hi, I'm Alin Trodden. I write texts, read books, and look for impressions. And I'm not bad at telling you about it. I am always happy to participate in interesting projects."

Comments (0)

This article has no comment, be the first!

Add comment

Related News

Game 7 days to die - system requirements and possible problems.

Game 7 days to die - system requirements and possible problems.

With every year comes more and more games released studios. Was no exception and 7 days to die, which, despite the fact that quickly gained popularity, was created on the simplest of public Unity engine. But today we will not disc...

What gives archaeology in

What gives archaeology in "world war II"?

it's No secret that the Burning Legion gave Azeroth a lot of surprises. The fighting that broke out on the broken Isles, were not the only outcome of the attack of army of darkness. Along with the "Cataclysm" everyone got an oppor...

Atieclxx.exe: what's the process, and whether it is possible to get rid of it?

Atieclxx.exe: what's the process, and whether it is possible to get rid of it?

Very often in "task Manager" users of Windows systems see a lot of unknown processes. Despite the fact that today viral activity increased, how ever, not all services can be referred to as such. In particular, this appli...

Status in

Status in "You": beautiful sayings for good mood

Tired of the abundance of bad news in the social networks? Want to decorate your profile funny and original statement? Then the status in "You", collected in the article will help with this. In a good mood, as we know, life is eas...

"Sims 3: Medieval". Quests, quests and their passage in "the Sims: Medieval"

“the Sims 3: Medieval” – a fascinating life simulation that will immerse the player in the fascinating and dangerous world of kings, knights, pirates, mages and bards.History simulatorIt was developed by a team a...

"Skyrim: Sky Haven Temple". The passage locations, tips and advice

In this guide you will describe a few of the quest of the main storyline of the game ‘Skyrim: sky haven Temple”. In addition, location is often called the ‘Wall of Alduin”. Here we will discuss about where ...