Truncate last n characters in a string using .format() - python-3.x

I'm trying to truncate last 100 chars of a string using .format() and add another 100 chars long string. I thought this should be the most obvious and pythonic way:
print('{:.-100}{:100}'.format(str1, str2))
But it doesn't work.
I can achieve it this way:
print('{}{:100}'.format(str1[:-100], str2))
But I don't think it's beautiful or readable code. Of course, I can use .replace() and .strip() and .ljust() but...
So am I missing something in my original broken code? Or shall I stay with the second one? Or will it be the most pythonic way to use .replace() etc?

You can do:
print('{}{}'.format(str1[:-100], str2[:100]))
At least that way, you will be consistent.
If you want other ways to do it, this question might help: Left truncate using python 3.5 str.format? .

If I was at your place, I would accept the second one. It looks readable to me. I always truncate strings like how you did.

Related

Excel function to get chars between hyphens

I have a string like:
AB-CD-EF-GH-IK
I wanna get EF between second and third hyphen.
Please help me to figure it out, Thanks
Even shorter is:
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),2*LEN(A1),LEN(A1)))
Regards
This will work with varying lengths of strings between the dashes. Doesn't look pretty but works.
=LEFT(REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""),FIND("-",REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""))-1)
Not because this is the right approach, but because shorter than (what was at the time!) the accepted Answer:
=MID(A6,FIND("-",A6,FIND("-",A6)+1)+1,FIND("-",A6,FIND("-",A6,FIND("-",A6)+1)+1)-FIND("-",A6,FIND("-",A6)+1)-1)
A small point in its favour may be that it uses only two common-or-garden functions:
MID to extract the string
FIND to find the index numbers of the relevant characters.

How to capture a string between parentheses?

str = "fa, (captured)[asd] asf, 31"
for word in str:gmatch("\(%a+\)") do
print(word)
end
Hi! I want to capture a word between parentheses.
My Code should print "captured" string.
lua: /home/casey/Desktop/test.lua:3: invalid escape sequence near '\('
And i got this syntax error.
Of course, I can just find position of parentheses and use string.sub function
But I prefer simple code.
Also, brackets gave me a similar error.
The escape character in Lua patterns is %, not \. So use this:
word=str:match("%((%a+)%)")
If you only need one match, there is no need for a gmatch loop.
To capture the string in square brackets, use a similar pattern:
word=str:match("%[(%a+)%]")
If the captured string is not entirely composed of letters, use .- instead of %a+.
lhf's answer likely gives you what you need, but I'd like to mention one more option that I feel is underused and may work for you as well. One issue with using %((%a+)%) is that it doesn't work for nested parentheses: if you apply it to something like "(text(more)text)", you'll get "more" even though you may expect "text(more)text". Note that you can't fix it by asking to match to the first closing parenthesis (%(([^%)]+)%)) as it will give you "text(more".
However, you can use %bxy pattern item, which balances x and y occurrences and will return (text(more)text) in this case (you'd need to use something like (%b()) to capture it). Again, this may be overkill for your case, but useful to keep in mind and may help someone else who comes across this problem.

Add parenthesis to vim string

I'm having a bit of trouble using parenthesis in a vim string. I just need to add a set of parenthesis around 3 digits, but I can't seem to find where I'm suppose to correctly place them. So for example; I would have to place them around a phone number such as: 2015551212.
Right now I have a strings that separates the numbers and puts a hyphen between them. For example; 201 555-1212. So I just need the parenthesis. The final result should look like: (201) 555-1212
The string I have so far is this: s/\(\d\{3}\)\(\d\{3}\)/\1 \2-/g
How might I go about doing this?
Thanks
Just add the parens around the \1 in your replacement.
s/\(\d\{3\}\)\(\d\{3\}\)/(\1) \2-/g
If you want to go in reverse, and change "(800) 555-1212" to "8005551212", you can use something like this:
s/(\(\d\d\d\))\ \(\d\d\d\)-\(\d\d\d\d\)/\1\2\3/g
Instead of the \d\d\d, you could use \d\{3\}, but that is more trouble to type.

String compare : Comparing 'zürich' and 'zurich' results in -1

I'm trying to do a string compare for 'zürich' and 'zurich'
Something like this:
int compareResult = String.Compare(zürich, zurich);
So what happens is that it returns -1, which causes a problem as I'm using compareResult for an if-else later.
Can someone point me to the right direction on why does this happen. Do I need to clean this first before comparing "zürich" or is it something else?
you use the method just fine, but the strings are actually different.
so, in order to make this comparison in your way, you need:
decide if this you want every comparison that uses ü and other "special" latin characters to look at them as they were the simple characters.
i.e. in every time you see ü, it will treat it as a "u"
if so, you need to do pre-processing of both the strings, and replace all special chars with regular ones.
there is another thread about it here:
How can I remove accents on a string?
hope it helped.

Complex replacement in gVim

I have been a terrible person as of late when it comes to Minecraft. I have over-modded it to the point that I need to completely re-write the IDs of them all.
The only problem is that... It'll take about a couple of hours jut to re-write them ONCE, not to mention if any of them collide with the original game. So, in order to save time, I figured I'd use Vim, but after reading through several of the helpful posts on here, I still only know a minimal amount about the replacement feature/command. Here's what I'm trying to do:
Replace this
I:exampleModnamePath.id=16389
I:exampleModnamePat2.id=19657
Etc.
With this
I:exampleModnamePath.id=20000
I:exampleModnamePath.id=20001
Etc.
This continues for a while, and to those who answer, could you please inform me of how it works, so I don't have to ask these questions all the time?
For your perusal:
:let g:num = 1
:g/\.id=\d\+$/exec 's!\.id=\d\+$!.id='.g:num.'! | let g:num=g:num+1'
This is slightly simplified version of my code for (re)numbering chapters in the ebooks.
Idea in a nutshell: use :g to run something over affected lines; use :exec to generate/run new substitution command AND increment the counter. Tried it once and was surprised to find that the trick worked. Was inspired by my previous toying with :g//s/// combo.
I'm not sure what is the rule you are using to choose which number to use for replacement but if all you need
is just a new number that doesn't collide with previous ones you could try just replacing the first digit
with something in a range not used. Something like replacing 16389 with 76389
To do that you could use this :s/Path.id=.\(.*\)/Path.id=7\1
That would search for the string Path.id= followed by a single character and then a group of more characters.
I will replace it with the string Path.id=7 and the group previously selected.
You could make it more selectiv adding letters before Path.id to match only certain types of paths.

Resources